home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / src-16f.lha / code / debug-int.lisp < prev    next >
Encoding:
Text File  |  1992-08-02  |  146.5 KB  |  3,899 lines

  1. ;;; -*- Mode: completion; Log: code.log; Package: debug-internals -*-
  2. ;;;
  3. ;;; **********************************************************************
  4. ;;; This code was written as part of the CMU Common Lisp project at
  5. ;;; Carnegie Mellon University, and has been placed in the public domain.
  6. ;;; If you want to use this code or any part of CMU Common Lisp, please contact
  7. ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
  8. ;;;
  9. (ext:file-comment
  10.   "$Header: debug-int.lisp,v 1.50.1.1 92/08/02 12:02:53 ram Exp $")
  11. ;;;
  12. ;;; **********************************************************************
  13. ;;;
  14. ;;; This file contains the implementation of the programmer's interface
  15. ;;; to writing debugging tools.
  16. ;;;
  17. ;;; Written by Bill Chiles and Rob Maclachlan.
  18. ;;;
  19.  
  20. (in-package "DEBUG-INTERNALS" :nicknames '("DI"))
  21.  
  22.  
  23. ;;; The compiler's debug-source structure is almost exactly what we want, so
  24. ;;; just get these symbols and export them.
  25. ;;;
  26. (import '(c::debug-source-from c::debug-source-name c::debug-source-created
  27.       c::debug-source-compiled c::debug-source-start-positions
  28.       c::make-debug-source c::debug-source c::debug-source-p))
  29.  
  30. (export '(debug-variable-name debug-variable-package debug-variable-symbol
  31.       debug-variable-id debug-variable-value debug-variable-validity
  32.       debug-variable-valid-value debug-variable debug-variable-p
  33.  
  34.       top-frame frame-down frame-up flush-frames-above frame-debug-function
  35.       frame-code-location eval-in-frame return-from-frame frame-catches
  36.       frame-number frame frame-p
  37.  
  38.       do-debug-function-blocks debug-function-lambda-list
  39.       debug-variable-info-available do-debug-function-variables
  40.       debug-function-symbol-variables ambiguous-debug-variables
  41.       preprocess-for-eval function-debug-function debug-function-function
  42.       debug-function-kind debug-function-name debug-function
  43.       debug-function-p debug-function-start-location
  44.  
  45.       do-debug-block-locations debug-block-successors debug-block
  46.       debug-block-p debug-block-elsewhere-p
  47.  
  48.       make-breakpoint activate-breakpoint deactivate-breakpoint
  49.       breakpoint-active-p breakpoint-hook-function breakpoint-info
  50.       breakpoint-kind breakpoint-what breakpoint breakpoint-p
  51.       delete-breakpoint function-end-cookie-valid-p
  52.  
  53.       code-location-debug-function code-location-debug-block
  54.       code-location-top-level-form-offset code-location-form-number
  55.       code-location-debug-source code-location-kind
  56.       code-location code-location-p code-location-unknown-p code-location=
  57.  
  58.       debug-source-from debug-source-name debug-source-created
  59.       debug-source-compiled debug-source-root-number
  60.       debug-source-start-positions form-number-translations
  61.       source-path-context debug-source debug-source-p
  62.  
  63.       debug-condition no-debug-info no-debug-function-returns
  64.       no-debug-blocks lambda-list-unavailable
  65.  
  66.       debug-error unhandled-condition invalid-control-stack-pointer
  67.       unknown-code-location unknown-debug-variable invalid-value
  68.       ambiguous-variable-name frame-function-mismatch
  69.  
  70.       set-breakpoint-for-editor set-location-breakpoint-for-editor
  71.       delete-breakpoint-for-editor
  72.  
  73.       *debugging-interpreter*))
  74.  
  75.  
  76.  
  77. ;;;; Conditions.
  78.  
  79. ;;; The interface to building debugging tools signals conditions that prevent
  80. ;;; it from adhering to its contract.  These are serious-conditions because the
  81. ;;; program using the interface must handle them before it can correctly
  82. ;;; continue execution.  These debugging conditions are not errors since it is
  83. ;;; no fault of the programmers that the conditions occur.  The interface does
  84. ;;; not provide for programs to detect these situations other than calling a
  85. ;;; routine that detects them and signals a condition.  For example,
  86. ;;; programmers call A which may fail to return successfully due to a lack of
  87. ;;; debug information, and there is no B the they could have called to realize
  88. ;;; A would fail.  It is not an error to have called A, but it is an error for
  89. ;;; the program to then ignore the signal generated by A since it cannot
  90. ;;; continue without A's correctly returning a value or performing some
  91. ;;; operation.
  92. ;;;
  93. ;;; Use DEBUG-SIGNAL to signal these conditions.
  94. ;;;
  95.  
  96. (define-condition debug-condition (serious-condition)
  97.   ()
  98.   (:documentation
  99.    "All debug-conditions inherit from this type.  These are serious conditions
  100.     that must be handled, but they are not programmer errors."))
  101.  
  102. (define-condition no-debug-info (debug-condition)
  103.   ()
  104.   (:documentation "There is absolutely no debugging information available.")
  105.   (:report (lambda (condition stream)
  106.          (declare (ignore condition))
  107.          (fresh-line stream)
  108.          (write-line "No debugging information available." stream))))
  109.  
  110. (define-condition no-debug-function-returns (debug-condition)
  111.   (debug-function)
  112.   (:documentation
  113.    "The system could not return values from a frame with debug-function since
  114.     it lacked information about returning values.")
  115.   (:report (lambda (condition stream)
  116.          (let ((fun (debug-function-function
  117.              (no-debug-function-returns-debug-function condition))))
  118.            (format stream
  119.                "~&Cannot return values from ~:[frame~;~:*~S~] since ~
  120.             the debug information lacks details about returning ~
  121.             values here."
  122.                fun)))))
  123.  
  124. (define-condition no-debug-blocks (debug-condition)
  125.   (debug-function)
  126.   (:documentation "The debug-function has no debug-block information.")
  127.   (:report (lambda (condition stream)
  128.          (format stream "~&~S has no debug-block information."
  129.              (no-debug-blocks-debug-function condition)))))
  130.  
  131. (define-condition no-debug-variables (debug-condition)
  132.   (debug-function)
  133.   (:documentation "The debug-function has no debug-variable information.")
  134.   (:report (lambda (condition stream)
  135.          (format stream "~&~S has no debug-variable information."
  136.              (no-debug-variables-debug-function condition)))))
  137.  
  138. (define-condition lambda-list-unavailable (debug-condition)
  139.   (debug-function)
  140.   (:documentation
  141.    "The debug-function has no lambda-list since argument debug-variables are
  142.     unavailable.")
  143.   (:report (lambda (condition stream)
  144.          (format stream "~&~S has no lambda-list information available."
  145.              (lambda-list-unavailable-debug-function condition)))))
  146.  
  147. (define-condition invalid-value (debug-condition)
  148.   ((debug-variable)
  149.    (frame))
  150.   (:report (lambda (condition stream)
  151.          (format stream "~&~S has :invalid or :unknown value in ~S."
  152.              (invalid-value-debug-variable condition)
  153.              (invalid-value-frame condition)))))
  154.  
  155. (define-condition ambiguous-variable-name (debug-condition)
  156.   ((name)
  157.    (frame))
  158.   (:report (lambda (condition stream)
  159.          (format stream "~&~S names more than one valid variable in ~S."
  160.              (ambiguous-variable-name-name condition)
  161.              (ambiguous-variable-name-frame condition)))))
  162.  
  163.  
  164. ;;;; Errors and DEBUG-SIGNAL.
  165.  
  166. ;;; The debug-internals code tries to signal all programmer errors as subtypes
  167. ;;; of debug-error.  There are calls to ERROR signalling simple-errors, but
  168. ;;; these dummy checks in the code and shouldn't come up.
  169. ;;;
  170. ;;; While under development, this code also signals errors in code branches
  171. ;;; that remain unimplemented.
  172. ;;;
  173.  
  174. (define-condition debug-error (error) ()
  175.   (:documentation
  176.    "All programmer errors from using the interface for building debugging
  177.     tools inherit from this type."))
  178.  
  179. (define-condition unhandled-condition (debug-error)
  180.   ((condition))
  181.   (:report (lambda (condition stream)
  182.          (format stream "~&Unhandled debug-condition:~%~A"
  183.              (unhandled-condition-condition condition)))))
  184.  
  185. (define-condition unknown-code-location (debug-error)
  186.   ((code-location))
  187.   (:report (lambda (condition stream)
  188.          (format stream "~&Invalid use of an unknown code-location -- ~S."
  189.              (unknown-code-location-code-location condition)))))
  190.  
  191. (define-condition unknown-debug-variable (debug-error)
  192.   ((debug-variable)
  193.    (debug-function))
  194.   (:report (lambda (condition stream)
  195.          (format stream "~&~S not in ~S."
  196.              (unknown-debug-variable-debug-variable condition)
  197.              (unknown-debug-variable-debug-function condition)))))
  198.  
  199. (define-condition invalid-control-stack-pointer (debug-error)
  200.   ()
  201.   (:report (lambda (condition stream)
  202.          (declare (ignore condition))
  203.          (fresh-line stream)
  204.          (write-string "Invalid control stack pointer." stream))))
  205.  
  206. (define-condition frame-function-mismatch (debug-error)
  207.   ((code-location)
  208.    (frame)
  209.    (form))
  210.   (:report (lambda (condition stream)
  211.          (format stream
  212.              "~&Form was preprocessed for ~S,~% but called on ~S:~%  ~S"
  213.              (frame-function-mismatch-code-location condition)
  214.              (frame-function-mismatch-frame condition)
  215.              (frame-function-mismatch-form condition)))))
  216.  
  217.  
  218. ;;; DEBUG-SIGNAL -- Internal.
  219. ;;;
  220. ;;; This signals debug-conditions.  If they go unhandled, then signal an
  221. ;;; unhandled-condition error.
  222. ;;;
  223. ;;; ??? Get SIGNAL in the right package!
  224. ;;;
  225. (defmacro debug-signal (datum &rest arguments)
  226.   `(let ((condition (make-condition ,datum ,@arguments)))
  227.      (signal condition)
  228.      (error 'unhandled-condition :condition condition)))
  229.  
  230.  
  231.  
  232. ;;;; Structures.
  233.  
  234. ;;; Most of these structures model information stored in internal data
  235. ;;; structures created by the compiler.  Whenever comments preface an object or
  236. ;;; type with "compiler", they refer to the internal compiler thing, not to the
  237. ;;; object or type with the same name in the "DI" package.
  238. ;;;
  239.  
  240.  
  241. ;;;
  242. ;;; Debug-variables
  243. ;;;
  244.  
  245. ;;; These exist for caching data stored in packed binary form in compiler
  246. ;;; debug-functions.  Debug-functions store these.
  247. ;;;
  248. (defstruct (debug-variable (:print-function print-debug-variable)
  249.                (:constructor nil))
  250.   ;;
  251.   ;; String name of variable.
  252.   (name nil :type simple-string)
  253.   ;;
  254.   ;; String name of package.  Nil when variable's name is uninterned.
  255.   (package nil :type (or null simple-string))
  256.   ;;
  257.   ;; Unique integer identification relative to other variables with the same
  258.   ;; name and package.
  259.   (id 0 :type c::index)
  260.   ;;
  261.   ;; Whether the variable always has a valid value.
  262.   (alive-p nil :type c::boolean))
  263.  
  264. (defun print-debug-variable (obj str n)
  265.   (declare (ignore n))
  266.   (format str "#<Debug-Variable ~A:~A:~A>"
  267.       (debug-variable-package obj)
  268.       (debug-variable-name obj)
  269.       (debug-variable-id obj)))
  270.  
  271. (setf (documentation 'debug-variable-name 'function)
  272.   "Returns the name of the debug-variable.  The name is the name of the symbol
  273.    used as an identifier when writing the code.")
  274.  
  275. (setf (documentation 'debug-variable-package 'function)
  276.   "Returns the package name of the debug-variable.  This is the package name of
  277.    the symbol used as an identifier when writing the code.")
  278.  
  279. (setf (documentation 'debug-variable-id 'function)
  280.   "Returns the integer that makes debug-variable's name and package name unique
  281.    with respect to other debug-variable's in the same function.")
  282.  
  283.  
  284. (defstruct (compiled-debug-variable
  285.         (:include debug-variable)
  286.         (:constructor make-compiled-debug-variable
  287.               (name package id alive-p sc-offset save-sc-offset)))
  288.   ;;
  289.   ;; Storage class and offset.  (unexported).
  290.   (sc-offset nil :type c::sc-offset)
  291.   ;;
  292.   ;; Storage class and offset when saved somewhere.
  293.   (save-sc-offset nil :type (or c::sc-offset null)))
  294.  
  295. (defstruct (interpreted-debug-variable
  296.         (:include debug-variable
  297.               (alive-p t))
  298.         (:constructor make-interpreted-debug-variable
  299.               (name package ir1-var)))
  300.   ;;
  301.   ;; This is the IR1 structure that holds information about interpreted vars.
  302.   (ir1-var nil :type c::lambda-var))
  303.  
  304. ;;;
  305. ;;; Frames
  306. ;;;
  307.  
  308. ;;; These represents call-frames on the stack.
  309. ;;;
  310. (defstruct (frame (:constructor nil))
  311.   ;;
  312.   ;; Next frame up.  Null when top frame.
  313.   (up nil :type (or frame null))
  314.   ;;
  315.   ;; Previous frame down.  Nil when the bottom frame.  Before computing the
  316.   ;; next frame down, this slot holds the frame pointer to the control stack
  317.   ;; for the given frame.  This lets us get the next frame down and the
  318.   ;; return-pc for that frame.
  319.   (%down :unparsed :type (or frame (member nil :unparsed)))
  320.   ;;
  321.   ;; Debug-function for function whose call this frame represents.
  322.   (debug-function nil :type debug-function)
  323.   ;;
  324.   ;; Code-location to continue upon return to frame.
  325.   (code-location nil :type code-location)
  326.   ;;
  327.   ;; A-list of catch-tags to code-locations.
  328.   (%catches :unparsed :type (or list (member :unparsed)))
  329.   ;;
  330.   ;; Pointer to frame on control stack.  (unexported)
  331.   ;; When is an interpreted-frame, this is an index into the interpreter's stack.
  332.   pointer
  333.   ;;
  334.   ;; This is the frame's number for prompt printing.  Top is zero.
  335.   number)
  336.  
  337. (setf (documentation 'frame-up 'function)
  338.   "Returns the frame immediately above frame on the stack.  When frame is
  339.    the top of the stack, this returns nil.")
  340.  
  341. (setf (documentation 'frame-debug-function 'function)
  342.   "Returns the debug-function for the function whose call frame represents.")
  343.  
  344. (setf (documentation 'frame-code-location 'function)
  345.   "Returns the code-location where the frame's debug-function will continue
  346.    running when program execution returns to this frame.  If someone
  347.    interrupted this frame, the result could be an unknown code-location.")
  348.  
  349.  
  350. (defstruct (compiled-frame
  351.         (:include frame)
  352.         (:print-function print-compiled-frame)
  353.         (:constructor make-compiled-frame
  354.               (pointer up debug-function code-location number
  355.                &optional escaped)))
  356.   ;;
  357.   ;; Indicates whether someone interrupted frame.  (unexported).
  358.   ;; If escaped, this is a pointer to the escape frame on the control stack.
  359.   escaped)
  360.  
  361. (defun print-compiled-frame (obj str n)
  362.   (declare (ignore n))
  363.   (format str "#<Compiled-Frame ~S~:[~;, interrupted~]>"
  364.       (debug-function-name (frame-debug-function obj))
  365.       (compiled-frame-escaped obj)))
  366.  
  367.  
  368. (defstruct (interpreted-frame
  369.         (:include frame)
  370.         (:print-function print-interpreted-frame)
  371.         (:constructor make-interpreted-frame
  372.               (pointer up debug-function code-location number
  373.                real-frame closure)))
  374.   ;;
  375.   ;; This points to the compiled-frame for EVAL:INTERNAL-APPLY-LOOP.
  376.   (real-frame nil :type compiled-frame)
  377.   ;;
  378.   ;; This is the closed over data used by the interpreter.
  379.   (closure nil :type simple-vector))
  380.  
  381. (defun print-interpreted-frame (obj str n)
  382.   (declare (ignore n))
  383.   (format str "#<Interpreted-Frame ~S>"
  384.       (debug-function-name (frame-debug-function obj))))
  385.  
  386. ;;;
  387. ;;; Debug-functions
  388. ;;;
  389.  
  390. ;;; These exist for caching data stored in packed binary form in compiler
  391. ;;; debug-functions.  *compiled-debug-functions* maps a c::debug-function to a
  392. ;;; debug-function.  There should only be one debug-function in existence for
  393. ;;; any function; that is, all code-locations and other objects that reference
  394. ;;; debug-functions point to unique objects.  This is due to the overhead in
  395. ;;; cached information.
  396. ;;;
  397. (defstruct (debug-function (:print-function print-debug-function))
  398.   ;;
  399.   ;; Some representation of the function arguments.  See
  400.   ;; DEBUG-FUNCTION-LAMBDA-LIST.
  401.   ;; NOTE: must parse vars before parsing arg list stuff.
  402.   (%lambda-list :unparsed)
  403.   ;;
  404.   ;; Cached debug-variable information.  (unexported).
  405.   ;; These are sorted by their name.
  406.   (debug-vars :unparsed :type (or simple-vector null (member :unparsed)))
  407.   ;;
  408.   ;; Cached debug-block information.  This is nil when we have tried to parse
  409.   ;; the packed binary info, but none is available.
  410.   (blocks :unparsed :type (or simple-vector null (member :unparsed)))
  411.   ;;
  412.   ;; The actual function if available.
  413.   (%function :unparsed :type (or null function (member :unparsed))))
  414.  
  415. (defun print-debug-function (obj str n)
  416.   (declare (ignore n))
  417.   (format str "#<~A-Debug-Function ~S>"
  418.       (etypecase obj
  419.         (compiled-debug-function "Compiled")
  420.         (interpreted-debug-function "Interpreted")
  421.         (bogus-debug-function "Bogus"))
  422.       (debug-function-name obj)))
  423.  
  424.  
  425. (defstruct (compiled-debug-function
  426.         (:include debug-function)
  427.         (:constructor %make-compiled-debug-function
  428.               (compiler-debug-fun component)))
  429.   ;;
  430.   ;; Compiler's dumped debug-function information.  (unexported).
  431.   (compiler-debug-fun nil :type c::compiled-debug-function)
  432.   ;;
  433.   ;; Code object.  (unexported).
  434.   component
  435.   ;;
  436.   ;; The :function-start breakpoint (if any) used to facilitate function
  437.   ;; end breakpoints.
  438.   (end-starter nil :type (or null breakpoint)))
  439.  
  440. ;;; This maps c::compiled-debug-functions to compiled-debug-functions, so we
  441. ;;; can get at cached stuff and not duplicate compiled-debug-function
  442. ;;; structures.
  443. ;;;
  444. (defvar *compiled-debug-functions* (make-hash-table :test #'eq))
  445.  
  446. ;;; MAKE-COMPILED-DEBUG-FUNCTION -- Internal.
  447. ;;;
  448. ;;; Makes a compiled-debug-function for a c::compiler-debug-function and its
  449. ;;; component.  This maps the latter to the former in
  450. ;;; *compiled-debug-functions*.  If there already is a compiled-debug-function,
  451. ;;; then this returns it from *compiled-debug-functions*.
  452. ;;;
  453. (defun make-compiled-debug-function (compiler-debug-fun component)
  454.   (or (gethash compiler-debug-fun *compiled-debug-functions*)
  455.       (setf (gethash compiler-debug-fun *compiled-debug-functions*)
  456.         (%make-compiled-debug-function compiler-debug-fun component))))
  457.  
  458.  
  459. (defstruct (interpreted-debug-function
  460.         (:include debug-function)
  461.         (:constructor %make-interpreted-debug-function (ir1-lambda)))
  462.   ;;
  463.   ;; This is the ir1 lambda this debug-function represents.
  464.   (ir1-lambda nil :type c::clambda))
  465.  
  466. (defstruct (bogus-debug-function
  467.         (:include debug-function)
  468.         (:constructor make-bogus-debug-function
  469.               (%name &aux (%lambda-list nil) (debug-vars nil)
  470.                  (blocks nil) (%function nil))))
  471.   %name)
  472.  
  473. (defvar *ir1-lambda-debug-function* (make-hash-table :test #'eq))
  474.  
  475. (defun make-interpreted-debug-function (ir1-lambda)
  476.   (let ((home-lambda (c::lambda-home ir1-lambda)))
  477.     (or (gethash home-lambda *ir1-lambda-debug-function*)
  478.     (setf (gethash home-lambda *ir1-lambda-debug-function*)
  479.           (%make-interpreted-debug-function home-lambda)))))
  480.  
  481. ;;;
  482. ;;; Debug-blocks.
  483. ;;;
  484.  
  485. ;;; These exist for caching data stored in packed binary form in compiler
  486. ;;; debug-blocks.
  487. ;;;
  488. (defstruct (debug-block (:print-function print-debug-block))
  489.   ;;
  490.   ;; Code-locations where execution continues after this block.
  491.   (successors nil :type list)
  492.   ;;
  493.   ;; This indicates whether the block is a special glob of code shared by
  494.   ;; various functions and tucked away elsewhere in a component.  This kind of
  495.   ;; block has no start code-location.  In an interpreted-debug-block, this is
  496.   ;; always nil.  This slot is in all debug-blocks since it is an exported
  497.   ;; interface.
  498.   (elsewhere-p nil :type c::boolean))
  499.  
  500. (defun print-debug-block (obj str n)
  501.   (declare (ignore n))
  502.   (format str "#<~A-Debug-Block ~S>"
  503.       (etypecase obj
  504.         (compiled-debug-block "Compiled")
  505.         (interpreted-debug-block "Interpreted"))
  506.       (debug-block-function-name obj)))
  507.  
  508. (setf (documentation 'debug-block-successors 'function)
  509.   "Returns the list of possible code-locations where execution may continue
  510.    when the basic-block represented by debug-block completes its execution.")
  511.  
  512. (setf (documentation 'debug-block-elsewhere-p 'function)
  513.   "Returns whether debug-block represents elsewhere code.")
  514.  
  515.  
  516. (defstruct (compiled-debug-block (:include debug-block)
  517.                  (:constructor
  518.                   make-compiled-debug-block
  519.                   (code-locations successors elsewhere-p)))
  520.   ;;
  521.   ;; Code-location information for the block.
  522.   (code-locations nil :type simple-vector))
  523.  
  524. (defstruct (interpreted-debug-block (:include debug-block
  525.                           (elsewhere-p nil))
  526.                     (:constructor %make-interpreted-debug-block
  527.                           (ir1-block)))
  528.   ;;
  529.   ;; This is the IR1 block this debug-block represents.
  530.   (ir1-block nil :type c::cblock)
  531.   ;;
  532.   ;; Code-location information for the block.
  533.   (locations :unparsed :type (or (member :unparsed) simple-vector)))
  534.  
  535. (defvar *ir1-block-debug-block* (make-hash-table :test #'eq))
  536.  
  537. ;;; MAKE-INTERPRETED-DEBUG-BLOCK -- Internal.
  538. ;;;
  539. ;;; This makes a debug-block for the interpreter's ir1-block.  If we have it in
  540. ;;; the cache, return it.  If we need to make it, then first make debug-blocks
  541. ;;; for all the ir1-blocks in ir1-block's home lambda; this makes sure all the
  542. ;;; successors of ir1-block have debug-blocks.  We need this to fill in the
  543. ;;; resulting debug-block's successors list with debug-blocks, not ir1-blocks.
  544. ;;; After making all the possible debug-blocks we'll need to reference, go back
  545. ;;; over the list of new debug-blocks and fill in their successor slots with
  546. ;;; lists of debug-blocks.  Then look up our argument ir1-block to find its
  547. ;;; debug-block since we know we have it now.
  548. ;;;
  549. (defun make-interpreted-debug-block (ir1-block)
  550.   (check-type ir1-block c::cblock)
  551.   (let ((res (gethash ir1-block *ir1-block-debug-block*)))
  552.     (or res
  553.     (let ((lambda (c::block-home-lambda ir1-block)))
  554.       (c::do-blocks (block (c::block-component ir1-block))
  555.         (when (eq lambda (c::block-home-lambda block))
  556.           (push (setf (gethash block *ir1-block-debug-block*)
  557.               (%make-interpreted-debug-block block))
  558.             res)))
  559.       (dolist (block res)
  560.         (let* ((successors nil)
  561.            (cblock (interpreted-debug-block-ir1-block block))
  562.            (succ (c::block-succ cblock))
  563.            (valid-succ
  564.             (if (and succ
  565.                  (eq (car succ)
  566.                  (c::component-tail
  567.                   (c::block-component cblock))))
  568.             ()
  569.             succ)))
  570.           (dolist (sblock valid-succ)
  571.         (let ((dblock (gethash sblock *ir1-block-debug-block*)))
  572.           (when dblock
  573.             (push dblock successors))))
  574.           (setf (debug-block-successors block) (nreverse successors))))
  575.       (gethash ir1-block *ir1-block-debug-block*)))))
  576.  
  577. ;;;
  578. ;;; Breakpoints.
  579. ;;;
  580.  
  581. ;;; This is an internal structure that manages information about a breakpoint
  582. ;;; locations.  See *component-breakpoint-offsets*.
  583. ;;;
  584. (defstruct (breakpoint-data (:print-function print-breakpoint-data)
  585.                 (:constructor make-breakpoint-data
  586.                       (component offset)))
  587.   ;;
  588.   ;; This is the component in which the breakpoint lies.
  589.   component
  590.   ;;
  591.   ;; This is the byte offset into the component.
  592.   (offset nil :type c::index)
  593.   ;;
  594.   ;; The original instruction replaced by the breakpoint.
  595.   (instruction nil :type (or null (unsigned-byte 32)))
  596.   ;;
  597.   ;; This saves the sigmask while we execute the instruction replaced by
  598.   ;; a user break.  When we hit the after-breakpoint, we restore this mask.
  599.   (sigmask nil :type (or null (unsigned-byte 32)))
  600.   ;;
  601.   ;; A list of user breakpoints at this location.
  602.   (breakpoints nil :type list)
  603.   ;;
  604.   ;; An after-breakpoint might be at this location too.
  605.   (after-breakpoint nil :type (or null after-breakpoint)))
  606. ;;;
  607. (defun print-breakpoint-data (obj str n)
  608.   (declare (ignore n))
  609.   (format str "#<Breakpoint-Data ~S at ~S>"
  610.       (debug-function-name
  611.        (debug-function-from-pc (breakpoint-data-component obj)
  612.                    (breakpoint-data-offset obj)))
  613.       (breakpoint-data-offset obj)))
  614.   
  615. (defstruct (breakpoint (:print-function print-breakpoint)
  616.                (:constructor %make-breakpoint
  617.                      (hook-function what kind %info)))
  618.   ;;
  619.   ;; This is the function invoked when execution encounters the breakpoint.  It
  620.   ;; takes a frame, the breakpoint, and optionally a list of values.  Values
  621.   ;; are supplied for :function-end breakpoints as values to return for the
  622.   ;; function containing the breakpoint.  :function-end breakpoint
  623.   ;; hook-functions also take a cookie argument.  See cookie-fun slot.
  624.   (hook-function nil :type function)
  625.   ;;
  626.   ;; Code-location or debug-function.
  627.   (what nil :type (or code-location debug-function))
  628.   ;;
  629.   ;; :code-location, :function-start, or :function-end for that kind of
  630.   ;; breakpoint.  :unknown-return-partner if this is the partner of a
  631.   ;; :code-location breakpoint at an :unknown-return code-location.
  632.   (kind nil :type (member :code-location :function-start :function-end
  633.               :unknown-return-partner))
  634.   ;;
  635.   ;; Status helps the user and the implementation.
  636.   (status :inactive :type (member :active :inactive :deleted))
  637.   ;;
  638.   ;; This is a backpointer to a breakpoint-data.
  639.   (internal-data nil :type (or null breakpoint-data))
  640.   ;;
  641.   ;; With code-locations whose type is :unknown-return, there are really
  642.   ;; two breakpoints: one at the multiple-value entry point, and one at
  643.   ;; the single-value entry point.  This slot holds the breakpoint for the
  644.   ;; other one, or NIL if this isn't at an :unknown-return code location.
  645.   (unknown-return-partner nil :type (or null breakpoint))
  646.   ;;
  647.   ;; :function-end breakpoints use a breakpoint at the :function-start to
  648.   ;; establish the end breakpoint upon function entry.  We do this by frobbing
  649.   ;; the LRA to jump to a special piece of code that breaks and provides the
  650.   ;; return values for the returnee.  This slot points to the start breakpoint,
  651.   ;; so we can activate, deactivate, and delete it.
  652.   (start-helper nil :type (or null breakpoint))
  653.   ;;
  654.   ;; This is a hook users supply to get a dynamically unique cookie for
  655.   ;; identifying :function-end breakpoint executions.  That is, if there is one
  656.   ;; :function-end breakpoint, but there may be multiple pending calls of its
  657.   ;; function on the stack.  This function takes the cookie, and the
  658.   ;; hook-function takes the cookie too.
  659.   (cookie-fun nil :type (or null function))
  660.   ;;
  661.   ;; This slot users can set with whatever information they find useful.
  662.   %info)
  663. ;;;
  664. (defun print-breakpoint (obj str n)
  665.   (declare (ignore n))
  666.   (let ((what (breakpoint-what obj)))
  667.     (format str "#<Breakpoint ~S~:[~;~:*~S~]>"
  668.         (etypecase what
  669.           (code-location what)
  670.           (debug-function (debug-function-name what)))
  671.         (etypecase what
  672.           (code-location nil)
  673.           (debug-function (breakpoint-kind obj))))))
  674.  
  675. (setf (documentation 'breakpoint-hook-function 'function)
  676.   "Returns the breakpoint's function the system calls when execution encounters
  677.    the breakpoint, and it is active.  This is SETF'able.")
  678.  
  679. (setf (documentation 'breakpoint-what 'function)
  680.   "Returns the breakpoint's what specification.")
  681.  
  682. (setf (documentation 'breakpoint-kind 'function)
  683.   "Returns the breakpoint's kind specification.")
  684.  
  685. ;;; This is an internal structure.
  686. ;;;
  687. (defstruct (after-breakpoint (:print-function print-after-breakpoint)
  688.                  (:constructor
  689.                   make-after-breakpoint
  690.                   (previous-data internal-data &optional partner)))
  691.   ;;
  692.   ;; This is the data for the location which this after-breakpoint follows.
  693.   ;; When execution flows through this after-breakpoint, the breakpoint
  694.   ;; mechanism re-establishes the break instruction at this location, and
  695.   ;; replaces the after-breakpoint's break instruction with the instruction
  696.   ;; that should run as part of the original program.
  697.   (previous-data nil :type breakpoint-data)
  698.   ;;
  699.   ;; This is a backpointer to a breakpoint-data.
  700.   (internal-data nil :type (or null breakpoint-data))
  701.   ;;
  702.   ;; When breakpoint is where a branching instruction used to be, partner is an
  703.   ;; after-breakpoint at the other code location at which execution could have
  704.   ;; gone.  In addition to what is describe for the previous slot, the
  705.   ;; breakpoint mechanism has to remove partner as well as this
  706.   ;; after-breakpoint.
  707.   (partner nil :type (or null after-breakpoint)))
  708.  
  709. (defun print-after-breakpoint (obj str n)
  710.   (declare (ignore obj n))
  711.   (format str "#<After-Breakpoint ...>"))
  712.  
  713. ;;;
  714. ;;; Code-locations.
  715. ;;;
  716.  
  717. (defstruct (code-location (:print-function print-code-location)
  718.               (:constructor nil))
  719.   ;;
  720.   ;; This is the debug-function containing code-location.
  721.   (debug-function nil :type debug-function)
  722.   ;;
  723.   ;; This is initially :unsure.  Upon first trying to access an :unparsed slot,
  724.   ;; if the data is unavailable, then this becomes t, and the code-location is
  725.   ;; unknown.  If the data is available, this becomes nil, a known location.
  726.   ;; We can't use a separate type code-location for this since we must return
  727.   ;; code-locations before we can tell whether they're known or unknown.  For
  728.   ;; example, when parsing the stack, we don't want to unpack all the variables
  729.   ;; and blocks just to make frames.
  730.   (%unknown-p :unsure :type (member t nil :unsure))
  731.   ;;
  732.   ;; This is the debug-block containing code-location.
  733.   ;; Possibly toss this out and just find it in the blocks cache in
  734.   ;; debug-function.
  735.   (%debug-block :unparsed :type (or debug-block (member :unparsed)))
  736.   ;;
  737.   ;; This is the number of forms processed by the compiler or loader before
  738.   ;; the top-level form containing this code-location.
  739.   (%tlf-offset :unparsed :type (or c::index (member :unparsed)))
  740.   ;;
  741.   ;; This is the depth-first number of the node that begins code-location
  742.   ;; within its top-level form.
  743.   (%form-number :unparsed :type (or c::index (member :unparsed))))
  744.  
  745. (defun print-code-location (obj str n)
  746.   (declare (ignore n))
  747.   (format str "#<~A ~S>"
  748.       (ecase (code-location-unknown-p obj)
  749.         ((nil) (etypecase obj
  750.              (compiled-code-location "Compiled-Code-Location")
  751.              (interpreted-code-location "Interpreted-Code-Location")))
  752.         ((t) "Unknown-Code-Location"))
  753.       (debug-function-name (code-location-debug-function obj))))
  754.  
  755. (setf (documentation 'code-location-debug-function 'function)
  756.   "Returns the debug-function representing information about the function
  757.    corresponding to the code-location.")
  758.  
  759.  
  760. (defstruct (compiled-code-location
  761.         (:include code-location)
  762.         (:constructor make-known-code-location
  763.               (pc debug-function %tlf-offset %form-number
  764.                   %live-set kind &aux (%unknown-p nil)))
  765.         (:constructor make-compiled-code-location (pc debug-function)))
  766.   ;;
  767.   ;; This is an index into debug-function's component slot.
  768.   (pc nil :type c::index)
  769.   ;;
  770.   ;; This is a bit-vector indexed by a variable's position in
  771.   ;; DEBUG-FUNCTION-DEBUG-VARS indicating whether the variable has a valid
  772.   ;; value at this code-location.  (unexported).
  773.   (%live-set :unparsed :type (or simple-bit-vector (member :unparsed)))
  774.   ;;
  775.   ;; (unexported)
  776.   ;; To see c::location-kind, do "(kernel:type-expand 'c::location-kind)".
  777.   (kind :unparsed :type (or (member :unparsed) c::location-kind)))
  778.  
  779. (defstruct (interpreted-code-location
  780.         (:include code-location
  781.               (%unknown-p nil))
  782.         (:constructor make-interpreted-code-location
  783.               (ir1-node debug-function)))
  784.   ;;
  785.   ;; This is an index into debug-function's component slot.
  786.   (ir1-node nil :type c::node))
  787.   
  788.  
  789. ;;;
  790. ;;; Debug-sources
  791. ;;;
  792.  
  793. (proclaim '(inline debug-source-root-number))
  794. ;;;
  795. (defun debug-source-root-number (debug-source)
  796.   "Returns the number of top-level forms processed by the compiler before
  797.    compiling this source.  If this source is uncompiled, this is zero.  This
  798.    may be zero even if the source is compiled since the first form in the first
  799.    file compiled in one compilation, for example, must have a root number of
  800.    zero -- the compiler saw no other top-level forms before it."
  801.   (c::debug-source-source-root debug-source))
  802.  
  803. (setf (documentation 'c::debug-source-from 'function)
  804.   "Returns an indication of the type of source.  The following are the possible
  805.    values:
  806.       :file    from a file (obtained by COMPILE-FILE if compiled).
  807.       :lisp    from Lisp (obtained by COMPILE if compiled).
  808.       :stream  from a non-file stream.")
  809.  
  810. (setf (documentation 'c::debug-source-name 'function)
  811.   "Returns the actual source in some sense represented by debug-source, which
  812.    is related to DEBUG-SOURCE-FROM:
  813.       :file    the pathname of the file.
  814.       :lisp    a lambda-expression.
  815.       :stream  some descriptive string that's otherwise useless.")
  816.  
  817. (setf (documentation 'c::debug-source-created 'function)
  818.   "Returns the universal time someone created the source.  This may be nil if
  819.    it is unavailable.")
  820.  
  821. (setf (documentation 'c::debug-source-compiled 'function)
  822.   "Returns the time someone compiled the source.  This is nil if the source
  823.    is uncompiled.")
  824.  
  825. (setf (documentation 'c::debug-source-start-positions 'function)
  826.   "This function returns the file position of each top-level form as an array
  827.    if debug-source is from a :file.  If DEBUG-SOURCE-FROM is :lisp or :stream,
  828.    this returns nil.")
  829.  
  830. (setf (documentation 'c::debug-source-p 'function)
  831.   "Returns whether object is a debug-source.")
  832.  
  833.  
  834.  
  835. ;;;; Frames.
  836.  
  837. ;;; This is used in FIND-ESCAPE-FRAME and with the bogus components and LRAs
  838. ;;; used for :function-end breakpoints.  When a components debug-info slot is
  839. ;;; :bogus-lra, then the real-lra-slot contains the real component to continue
  840. ;;; executing, as opposed to the bogus component which appeared in some frame's
  841. ;;; LRA location.
  842. ;;;
  843. (defconstant real-lra-slot vm:code-constants-offset)
  844.  
  845. ;;; These are magically converted by the compiler.
  846. ;;;
  847. (defun kernel:current-sp () (kernel:current-sp))
  848. (defun kernel:current-fp () (kernel:current-fp))
  849. (defun kernel:stack-ref (s n) (kernel:stack-ref s n))
  850. (defun kernel:%set-stack-ref (s n value) (kernel:%set-stack-ref s n value))
  851. (defun kernel:function-code-header (fun) (kernel:function-code-header fun))
  852. (defun kernel:lra-code-header (lra) (kernel:lra-code-header lra))
  853. (defun kernel:make-lisp-obj (value) (kernel:make-lisp-obj value))
  854. (defun kernel:get-lisp-obj-address (thing) (kernel:get-lisp-obj-address thing))
  855. (defun kernel:function-word-offset (fun) (kernel:function-word-offset fun))
  856. ;;;
  857. (defsetf kernel:stack-ref kernel:%set-stack-ref)
  858.  
  859. (proclaim '(inline cstack-pointer-valid-p))
  860. (defun cstack-pointer-valid-p (x)
  861.   (declare (type system:system-area-pointer x))
  862.   (and (system:sap< x (kernel:current-sp))
  863.        (system:sap<= (alien:alien-sap (alien:extern-alien "control_stack"
  864.                               (* t)))
  865.              x)))
  866.  
  867. ;;; TOP-FRAME -- Public.
  868. ;;;
  869. (defun top-frame ()
  870.   "Returns the top frame of the control stack as it was before calling this
  871.    function."
  872.   (multiple-value-bind (fp pc)
  873.                (kernel:%caller-frame-and-pc)
  874.     (possibly-an-interpreted-frame
  875.      (compute-calling-frame (system:int-sap (* (ext:truly-the fixnum fp)
  876.                            vm:word-bytes))
  877.                 pc nil)
  878.      nil)))
  879.  
  880. ;;; FLUSH-FRAMES-ABOVE -- public.
  881. ;;; 
  882. (defun flush-frames-above (frame)
  883.   "Flush all of the frames above FRAME, and renumber all the frames below
  884.    FRAME."
  885.   (setf (frame-up frame) nil)
  886.   (do ((number 0 (1+ number))
  887.        (frame frame (frame-%down frame)))
  888.       ((not (frame-p frame)))
  889.     (setf (frame-number frame) number)))
  890.  
  891. ;;; FRAME-DOWN -- Public.
  892. ;;;
  893. ;;; We have to access the old-fp and return-pc out of frame and pass them to
  894. ;;; COMPUTE-CALLING-FRAME.
  895. ;;;
  896. (defun frame-down (frame)
  897.   "Returns the frame immediately below frame on the stack.  When frame is
  898.    the bottom of the stack, this returns nil."
  899.   (let ((down (frame-%down frame)))
  900.     (if (eq down :unparsed)
  901.     (let* ((real (frame-real-frame frame))
  902.            (debug-fun (frame-debug-function real)))
  903.       (setf (frame-%down frame)
  904.         (etypecase debug-fun
  905.           (compiled-debug-function
  906.            (let ((c-d-f (compiled-debug-function-compiler-debug-fun
  907.                  debug-fun)))
  908.              (possibly-an-interpreted-frame
  909.               (compute-calling-frame
  910.                (system:int-sap
  911.             (* (get-context-value
  912.                 real vm::ocfp-save-offset
  913.                 (c::compiled-debug-function-old-fp c-d-f))
  914.                vm:word-bytes))
  915.                (get-context-value
  916.             real vm::lra-save-offset
  917.             (c::compiled-debug-function-return-pc c-d-f))
  918.                frame)
  919.               frame)))
  920.           (bogus-debug-function
  921.            (let ((fp (frame-pointer real)))
  922.              (compute-calling-frame
  923.               (system:sap-ref-sap fp (* vm::ocfp-save-offset
  924.                         vm:word-bytes))
  925.               (kernel:stack-ref fp vm::lra-save-offset)
  926.               frame))))))
  927.     down)))
  928.  
  929.  
  930. ;;; GET-CONTEXT-VALUE  --  Internal.
  931. ;;;
  932. ;;; Get the old FP or return PC out of frame.  Stack-slot is the standard save
  933. ;;; location offset on the stack.  Loc is the saved sc-offset describing the
  934. ;;; main location.
  935. ;;;
  936. (defun get-context-value (frame stack-slot loc)
  937.   (declare (type compiled-frame frame) (type unsigned-byte stack-slot)
  938.        (type c::sc-offset loc))
  939.   (let ((pointer (frame-pointer frame))
  940.     (escaped (compiled-frame-escaped frame)))
  941.     (if escaped
  942.     (sub-access-debug-var-slot pointer loc escaped)
  943.     (kernel:stack-ref pointer stack-slot))))
  944. ;;;
  945. (defun (setf get-context-value) (value frame stack-slot loc)
  946.   (declare (type compiled-frame frame) (type unsigned-byte stack-slot)
  947.        (type c::sc-offset loc))
  948.   (let ((pointer (frame-pointer frame))
  949.     (escaped (compiled-frame-escaped frame)))
  950.     (if escaped
  951.     (sub-set-debug-var-slot pointer loc value escaped)
  952.     (setf (kernel:stack-ref pointer stack-slot) value))))
  953.  
  954.  
  955. (defvar *debugging-interpreter* nil
  956.   "When set, the debugger foregoes making interpreted-frames, so you can
  957.    debug the functions that manifest the interpreter.")
  958.  
  959. ;;; POSSIBLY-AN-INTERPRETED-FRAME -- Internal.
  960. ;;;
  961. ;;; This takes a newly computed frame, frame, and the frame above it on the
  962. ;;; stack, up-frame, which is possibly nil.  Frame is nil when we hit the
  963. ;;; bottom of the control stack.  When frame represents a call to
  964. ;;; EVAL::INTERNAL-APPLY-LOOP, we make an interpreted frame to replace frame.
  965. ;;; The interpreted frame points to frame.
  966. ;;;
  967. (defun possibly-an-interpreted-frame (frame up-frame)
  968.   (if (or (not frame)
  969.       (not (eq (debug-function-name (frame-debug-function frame))
  970.            'eval::internal-apply-loop))
  971.       *debugging-interpreter*
  972.       (compiled-frame-escaped frame))
  973.       frame
  974.       (flet ((get-var (name location)
  975.            (let ((vars (di:ambiguous-debug-variables
  976.                 (di:frame-debug-function frame) name)))
  977.          (when (or (null vars) (> (length vars) 1))
  978.            (error "Zero or more than one ~A variable in ~
  979.                EVAL::INTERNAL-APPLY-LOOP?"
  980.               (string-downcase name)))
  981.          (if (eq (debug-variable-validity (car vars) location)
  982.              :valid)
  983.              (car vars)))))
  984.     (let* ((code-loc (frame-code-location frame))
  985.            (ptr-var (get-var "FRAME-PTR" code-loc))
  986.            (node-var (get-var "NODE" code-loc))
  987.            (closure-var (get-var "CLOSURE" code-loc)))
  988.       (if (and ptr-var node-var closure-var)
  989.           (let* ((node (debug-variable-value node-var frame))
  990.              (d-fun (make-interpreted-debug-function
  991.                  (c::block-home-lambda (c::node-block node)))))
  992.         (make-interpreted-frame
  993.          (debug-variable-value ptr-var frame)
  994.          up-frame
  995.          d-fun
  996.          (make-interpreted-code-location node d-fun)
  997.          (frame-number frame)
  998.          frame
  999.          (debug-variable-value closure-var frame)))
  1000.           frame)))))
  1001.  
  1002.  
  1003. ;;; COMPUTE-CALLING-FRAME -- Internal.
  1004. ;;;
  1005. ;;; This returns a frame for the one existing in time immediately prior to the
  1006. ;;; frame referenced by current-fp.  This is current-fp's caller or the next
  1007. ;;; frame down the control stack.  If there is no down frame, this returns nil
  1008. ;;; for the bottom of the stack.  Up-frame is the up link for the resulting
  1009. ;;; frame object, and it is nil when we call this to get the top of the stack.
  1010. ;;;
  1011. ;;; The current frame contains the pointer to the temporally previous frame we
  1012. ;;; want, and the current frame contains the pc at which we will continue
  1013. ;;; executing upon returning to that previous frame.
  1014. ;;;
  1015. ;;; Note: Sometimes LRA is actually a fixnum.  This happens when lisp calls
  1016. ;;; into C.  In this case, the code object is stored on the stack after the
  1017. ;;; LRA, and the LRA is the word offset.
  1018. ;;; 
  1019. (defun compute-calling-frame (caller lra up-frame)
  1020.   (declare (type system:system-area-pointer caller))
  1021.   (when (cstack-pointer-valid-p caller)
  1022.     (multiple-value-bind
  1023.     (code pc-offset escaped)
  1024.     (if lra
  1025.         (multiple-value-bind
  1026.         (word-offset code)
  1027.         (if (ext:fixnump lra)
  1028.             (let ((fp (frame-pointer up-frame)))
  1029.               (values lra
  1030.                   (kernel:stack-ref fp (1+ vm::lra-save-offset))))
  1031.             (values (kernel:get-header-data lra)
  1032.                 (kernel:lra-code-header lra)))
  1033.           (if code
  1034.           (values code
  1035.               (* (1+ (- word-offset (kernel:get-header-data code)))
  1036.                  vm:word-bytes)
  1037.               nil)
  1038.           (values :foreign-function
  1039.               0
  1040.               nil)))
  1041.         (find-escaped-frame caller))
  1042.       (if (eq (kernel:code-debug-info code) :bogus-lra)
  1043.       (let ((real-lra (kernel:code-header-ref code real-lra-slot)))
  1044.         (compute-calling-frame caller real-lra up-frame))
  1045.       (let ((d-fun (case code
  1046.              (:undefined-function
  1047.               (make-bogus-debug-function
  1048.                "The Undefined Function"))
  1049.              (:foreign-function
  1050.               (make-bogus-debug-function
  1051.                "Foreign function call land"))
  1052.              ((nil)
  1053.               (make-bogus-debug-function
  1054.                "Bogus stack frame"))
  1055.              (t
  1056.               (debug-function-from-pc code pc-offset)))))
  1057.         (make-compiled-frame caller up-frame d-fun
  1058.                  (code-location-from-pc d-fun pc-offset
  1059.                             escaped)
  1060.                  (if up-frame (1+ (frame-number up-frame)) 0)
  1061.                  escaped))))))
  1062.  
  1063. (defun find-escaped-frame (frame-pointer)
  1064.   (declare (type system:system-area-pointer frame-pointer))
  1065.   (dotimes (index lisp::*free-interrupt-context-index* (values nil 0 nil))
  1066.     (alien:with-alien
  1067.     ((lisp-interrupt-contexts (array (* unix:sigcontext) nil) :extern))
  1068.       (let ((scp (alien:deref lisp-interrupt-contexts index)))
  1069.     (when (= (system:sap-int frame-pointer)
  1070.          (vm:sigcontext-register scp vm::cfp-offset))
  1071.       (system:without-gcing
  1072.        (let ((code (code-object-from-bits
  1073.             (vm:sigcontext-register scp vm::code-offset))))
  1074.          (when (symbolp code)
  1075.            (return (values code 0 scp)))
  1076.          (let* ((code-header-len (* (kernel:get-header-data code)
  1077.                     vm:word-bytes))
  1078.             (pc-offset
  1079.              (- (system:sap-int (alien:slot scp 'unix:sc-pc))
  1080.             (- (kernel:get-lisp-obj-address code)
  1081.                vm:other-pointer-type)
  1082.             code-header-len)))
  1083.            ;; Check to see if we were executing in a branch delay slot.
  1084.            #+pmax  ; pmax only
  1085.            (when (logbitp 31 (alien:slot scp 'mips::sc-cause))
  1086.          (incf pc-offset vm:word-bytes))
  1087.            (unless (<= 0 pc-offset
  1088.                (* (kernel:code-header-ref code
  1089.                               vm:code-code-size-slot)
  1090.                   vm:word-bytes))
  1091.          ;; We were in an assembly routine.  Therefore, use the LRA as
  1092.          ;; the pc.
  1093.          (setf pc-offset
  1094.                (- (vm:sigcontext-register scp vm::lra-offset)
  1095.               (kernel:get-lisp-obj-address code)
  1096.               code-header-len)))
  1097.            (return
  1098.         (if (eq (kernel:code-debug-info code) :bogus-lra)
  1099.             (let ((real-lra (kernel:code-header-ref code
  1100.                                 real-lra-slot)))
  1101.               (values (kernel:lra-code-header real-lra)
  1102.                   (kernel:get-header-data real-lra)
  1103.                   nil))
  1104.             (values code pc-offset scp)))))))))))
  1105.  
  1106. ;;; CODE-OBJECT-FROM-BITS  --  internal.
  1107. ;;;
  1108. ;;; Find the code object corresponding to the object represented by bits and
  1109. ;;; return it.  We assume bogus functions correspond to the
  1110. ;;; undefined-function.
  1111. ;;; 
  1112. (defun code-object-from-bits (bits)
  1113.   (declare (type (unsigned-byte 32) bits))
  1114.   (let ((object (kernel:make-lisp-obj bits)))
  1115.     (if (functionp object)
  1116.     (or (kernel:function-code-header object)
  1117.         :undefined-function)
  1118.     (let ((lowtag (kernel:get-lowtag object)))
  1119.       (if (= lowtag vm:other-pointer-type)
  1120.           (let ((type (kernel:get-type object)))
  1121.         (cond ((= type vm:code-header-type)
  1122.                object)
  1123.               ((= type vm:return-pc-header-type)
  1124.                (kernel:lra-code-header object))
  1125.               (t
  1126.                nil))))))))
  1127.  
  1128. ;;;
  1129. ;;; Frame utilities.
  1130. ;;;
  1131.  
  1132. ;;; DEBUG-FUNCTION-FROM-PC -- Internal.
  1133. ;;;
  1134. ;;; This returns a compiled-debug-function for code and pc.  We fetch the
  1135. ;;; c::debug-info and run down its function-map to get a
  1136. ;;; c::compiled-debug-function from the pc.  The result only needs to reference
  1137. ;;; the component, for function constants, and the c::compiled-debug-function.
  1138. ;;;
  1139. (defun debug-function-from-pc (component pc)
  1140.   (let ((info (kernel:code-debug-info component)))
  1141.     (cond
  1142.      ((not info)
  1143.       (debug-signal 'no-debug-info))
  1144.      ((eq info :bogus-lra)
  1145.       (make-bogus-debug-function "Function End Breakpoint"))
  1146.      (t
  1147.       (let* ((function-map (get-debug-info-function-map info))
  1148.          (len (length function-map)))
  1149.     (declare (simple-vector function-map))
  1150.     (if (= len 1)
  1151.         (make-compiled-debug-function (svref function-map 0) component)
  1152.         (let ((i 1)
  1153.           (elsewhere-p
  1154.            (>= pc (c::compiled-debug-function-elsewhere-pc
  1155.                (svref function-map 0)))))
  1156.           (declare (type c::index i))
  1157.           (loop
  1158.         (when (or (= i len)
  1159.               (< pc (if elsewhere-p
  1160.                     (c::compiled-debug-function-elsewhere-pc
  1161.                      (svref function-map (1+ i)))
  1162.                     (svref function-map i))))
  1163.           (return (make-compiled-debug-function
  1164.                (svref function-map (1- i))
  1165.                component)))
  1166.         (incf i 2)))))))))
  1167.  
  1168. ;;; CODE-LOCATION-FROM-PC -- Internal.
  1169. ;;;
  1170. ;;; This returns a code-location for the compiled-debug-function, debug-fun,
  1171. ;;; and the pc into its code vector.  If we stopped at a breakpoint, find
  1172. ;;; the code-location for that breakpoint.  Otherwise, make an :unsure code
  1173. ;;; location, so it can be filled in when we figure out what is going on.
  1174. ;;;
  1175. (defun code-location-from-pc (debug-fun pc escaped)
  1176.   (or (and (compiled-debug-function-p debug-fun)
  1177.        escaped
  1178.        (let ((data (breakpoint-data
  1179.             (compiled-debug-function-component debug-fun)
  1180.             pc nil)))
  1181.          (when (and data (breakpoint-data-breakpoints data))
  1182.            (let ((what (breakpoint-what
  1183.                 (first (breakpoint-data-breakpoints data)))))
  1184.          (when (compiled-code-location-p what)
  1185.            what)))))
  1186.       (make-compiled-code-location pc debug-fun)))
  1187.  
  1188. ;;; FRAME-CATCHES -- Public.
  1189. ;;;
  1190. (defun frame-catches (frame)
  1191.   "Returns an a-list mapping catch tags to code-locations.  These are
  1192.    code-locations at which execution would continue with frame as the top
  1193.    frame if someone threw to the corresponding tag."
  1194.   (let ((catch (system:int-sap (* lisp::*current-catch-block* vm:word-bytes)))
  1195.     (res nil)
  1196.     (fp (frame-pointer (frame-real-frame frame))))
  1197.     (loop
  1198.       (when (zerop (sap-int catch)) (return (nreverse res)))
  1199.       (when (sap= fp
  1200.           (system:int-sap
  1201.            (* (kernel:stack-ref catch vm:catch-block-current-cont-slot)
  1202.               vm:word-bytes)))
  1203.     (let* ((lra (kernel:stack-ref catch vm:catch-block-entry-pc-slot))
  1204.            (component
  1205.         (kernel:stack-ref catch vm:catch-block-current-code-slot))
  1206.            (word-offset (- (1+ (kernel:get-header-data lra))
  1207.                    (kernel:get-header-data component))))
  1208.       (push (cons (kernel:stack-ref catch vm:catch-block-tag-slot)
  1209.               (make-compiled-code-location
  1210.                (* word-offset vm:word-bytes)
  1211.                (frame-debug-function frame)))
  1212.         res)))
  1213.       (setf catch
  1214.         (system:sap-ref-sap catch
  1215.                 (* vm:catch-block-previous-catch-slot
  1216.                    vm:word-bytes))))))
  1217.  
  1218. ;;; FRAME-REAL-FRAME -- Internal.
  1219. ;;;
  1220. ;;; If an interpreted frame, return the real frame, otherwise frame.
  1221. ;;;
  1222. (defun frame-real-frame (frame)
  1223.   (etypecase frame
  1224.     (compiled-frame frame)
  1225.     (interpreted-frame (interpreted-frame-real-frame frame))))
  1226.  
  1227.  
  1228.  
  1229. ;;;; Debug-functions.
  1230.  
  1231. ;;; DO-DEBUG-FUNCTION-BLOCKS -- Public.
  1232. ;;;
  1233. (defmacro do-debug-function-blocks ((block-var debug-function &optional result)
  1234.                     &body body)
  1235.   "Executes the forms in a context with block-var bound to each debug-block in
  1236.    debug-function successively.  Result is an optional form to execute for
  1237.    return values, and DO-DEBUG-FUNCTION-BLOCKS returns nil if there is no
  1238.    result form.  This signals a no-debug-blocks condition when the
  1239.    debug-function lacks debug-block information."
  1240.   (let ((blocks (gensym))
  1241.     (i (gensym)))
  1242.     `(let ((,blocks (debug-function-debug-blocks ,debug-function)))
  1243.        (declare (simple-vector ,blocks))
  1244.        (dotimes (,i (length ,blocks) ,result)
  1245.      (let ((,block-var (svref ,blocks ,i)))
  1246.        ,@body)))))
  1247.  
  1248. ;;; DO-DEBUG-FUNCTION-VARIABLES -- Public.
  1249. ;;;
  1250. (defmacro do-debug-function-variables ((var debug-function &optional result)
  1251.                        &body body)
  1252.   "Executes body in a context with var bound to each debug-variable in
  1253.    debug-function.  This returns the value of executing result (defaults to
  1254.    nil).  This may iterate over only some of debug-function's variables or none
  1255.    depending on debug policy; for example, possibly the compilation only
  1256.    preserved argument information."
  1257.   (let ((vars (gensym))
  1258.     (i (gensym)))
  1259.     `(let ((,vars (debug-function-debug-variables ,debug-function)))
  1260.        (declare (type (or null simple-vector) ,vars))
  1261.        (if ,vars
  1262.        (dotimes (,i (length ,vars) ,result)
  1263.          (let ((,var (svref ,vars ,i)))
  1264.            ,@body))
  1265.        ,result))))
  1266.  
  1267. ;;; DEBUG-FUNCTION-FUNCTION -- Public.
  1268. ;;;
  1269. (defun debug-function-function (debug-function)
  1270.   "Returns the Common Lisp function associated with the debug-function.  This
  1271.    returns nil if the function is unavailable or is non-existent as a user
  1272.    callable function object."
  1273.   (let ((cached-value (debug-function-%function debug-function)))
  1274.     (if (eq cached-value :unparsed)
  1275.     (setf (debug-function-%function debug-function)
  1276.           (etypecase debug-function
  1277.         (compiled-debug-function
  1278.          (let ((component
  1279.             (compiled-debug-function-component debug-function))
  1280.                (start-pc
  1281.             (c::compiled-debug-function-start-pc
  1282.              (compiled-debug-function-compiler-debug-fun
  1283.               debug-function))))
  1284.            (do ((entry (system:%primitive code-entry-points component)
  1285.                    (system:%primitive function-next entry)))
  1286.                ((null entry) nil)
  1287.              (when (= start-pc
  1288.                   (c::compiled-debug-function-start-pc
  1289.                    (compiled-debug-function-compiler-debug-fun
  1290.                 (function-debug-function entry))))
  1291.                (return entry)))))
  1292.         (interpreted-debug-function
  1293.          (c::lambda-eval-info-function
  1294.           (c::leaf-info
  1295.            (interpreted-debug-function-ir1-lambda debug-function))))
  1296.         (bogus-debug-function nil)))
  1297.     cached-value)))
  1298.  
  1299.  
  1300. ;;; DEBUG-FUNCTION-NAME -- Public.
  1301. ;;;
  1302. (defun debug-function-name (debug-function)
  1303.   "Returns the name of the function represented by debug-function.  This may
  1304.    be a string or a cons; do not assume it is a symbol."
  1305.   (etypecase debug-function
  1306.     (compiled-debug-function
  1307.      (c::compiled-debug-function-name
  1308.       (compiled-debug-function-compiler-debug-fun debug-function)))
  1309.     (interpreted-debug-function
  1310.      (c::lambda-name (interpreted-debug-function-ir1-lambda debug-function)))
  1311.     (bogus-debug-function
  1312.      (bogus-debug-function-%name debug-function))))
  1313.  
  1314.  
  1315. ;;; FUNCTION-DEBUG-FUNCTION -- Public.
  1316. ;;;
  1317. (defun function-debug-function (fun)
  1318.   "Returns a debug-function that represents debug information for function."
  1319.   (if (eval:interpreted-function-p fun)
  1320.       (let ((eval-fun (eval::get-eval-function fun)))
  1321.     (make-interpreted-debug-function
  1322.      (or (eval::eval-function-definition eval-fun)
  1323.          (eval::convert-eval-fun eval-fun))))
  1324.       (let* ((name (system:%primitive c::function-name fun))
  1325.          (component (kernel:function-code-header fun))
  1326.          (res (find-if
  1327.            #'(lambda (x)
  1328.                (and (c::compiled-debug-function-p x)
  1329.                 (eq (c::compiled-debug-function-name x) name)
  1330.                 (eq (c::compiled-debug-function-kind x) nil)))
  1331.            (get-debug-info-function-map
  1332.             (kernel:code-debug-info component)))))
  1333.     (if res
  1334.         (make-compiled-debug-function res component)
  1335.         ;; This used to be the non-interpreted branch, but William wrote it
  1336.         ;; to return the debug-fun of fun's XEP instead of fun's debug-fun.
  1337.         ;; The above code does this more correctly, but it doesn't get or
  1338.         ;; eliminate all appropriate cases.  It mostly works, and probably
  1339.         ;; works for all named functions anyway.
  1340.         (debug-function-from-pc component
  1341.                     (* (- (kernel:function-word-offset fun)
  1342.                       (kernel:get-header-data component))
  1343.                        vm:word-bytes))))))
  1344.  
  1345.  
  1346. ;;; DEBUG-FUNCTION-KIND -- Public.
  1347. ;;;
  1348. (defun debug-function-kind (debug-function)
  1349.   "Returns the kind of the function which is one of :optional, :external,
  1350.    :top-level, :cleanup, nil."
  1351.   (etypecase debug-function
  1352.     (compiled-debug-function
  1353.      (c::compiled-debug-function-kind
  1354.       (compiled-debug-function-compiler-debug-fun debug-function)))
  1355.     (interpreted-debug-function
  1356.      (c::lambda-kind (interpreted-debug-function-ir1-lambda debug-function)))
  1357.     (bogus-debug-function
  1358.      nil)))
  1359.  
  1360. ;;; DEBUG-VARIABLE-INFO-AVAILABLE -- Public.
  1361. ;;;
  1362. (defun debug-variable-info-available (debug-function)
  1363.   "Returns whether there is any variable information for debug-function."
  1364.   (not (not (debug-function-debug-variables debug-function))))
  1365.  
  1366. ;;; DEBUG-FUNCTION-SYMBOL-VARIABLES -- Public.
  1367. ;;;
  1368. (defun debug-function-symbol-variables (debug-function symbol)
  1369.   "Returns a list of debug-variables in debug-function having the same name
  1370.    and package as symbol.  If symbol is uninterned, then this returns a list of
  1371.    debug-variables without package names and with the same name as symbol.  The
  1372.    result of this function is limited to the availability of variable
  1373.    information in debug-function; for example, possibly debug-function only
  1374.    knows about its arguments."
  1375.   (let ((vars (ambiguous-debug-variables debug-function (symbol-name symbol)))
  1376.     (package (if (symbol-package symbol)
  1377.              (package-name (symbol-package symbol)))))
  1378.     (delete-if (if (stringp package)
  1379.            #'(lambda (var)
  1380.                (let ((p (debug-variable-package var)))
  1381.              (or (not (stringp p))
  1382.                  (string/= p package))))
  1383.            #'(lambda (var)
  1384.                (stringp (debug-variable-package var))))
  1385.            vars)))
  1386.  
  1387. ;;; AMBIGUOUS-DEBUG-VARIABLES -- Public.
  1388. ;;;
  1389. (defun ambiguous-debug-variables (debug-function name-prefix-string)
  1390.    "Returns a list of debug-variables in debug-function whose names contain
  1391.     name-prefix-string as an intial substring.  The result of this function is
  1392.     limited to the availability of variable information in debug-function; for
  1393.     example, possibly debug-function only knows about its arguments."
  1394.   (declare (simple-string name-prefix-string))
  1395.   (let ((variables (debug-function-debug-variables debug-function)))
  1396.     (declare (type (or null simple-vector) variables))
  1397.     (if variables
  1398.     (let* ((len (length variables))
  1399.            (prefix-len (length name-prefix-string))
  1400.            (pos (find-variable name-prefix-string variables len))
  1401.            (res nil))
  1402.       (when pos
  1403.         ;; Find names from pos to variable's len that contain prefix.
  1404.         (do ((i pos (1+ i)))
  1405.         ((= i len))
  1406.           (let* ((var (svref variables i))
  1407.              (name (debug-variable-name var))
  1408.              (name-len (length name)))
  1409.         (declare (simple-string name))
  1410.         (when (/= (or (string/= name-prefix-string name
  1411.                     :end1 prefix-len :end2 name-len)
  1412.                   prefix-len)
  1413.               prefix-len)
  1414.           (return))
  1415.         (push var res)))
  1416.         (setq res (nreverse res)))
  1417.       res))))
  1418.  
  1419. ;;; FIND-VARIABLE -- Internal.
  1420. ;;;
  1421. ;;; This returns a position in variables for one containing name as an initial
  1422. ;;; substring.  End is the length of variables if supplied.
  1423. ;;;
  1424. (defun find-variable (name variables &optional end)
  1425.   (declare (simple-vector variables)
  1426.        (simple-string name))
  1427.   (let ((name-len (length name)))
  1428.     (position name variables
  1429.           :test #'(lambda (x y)
  1430.             (let* ((y (debug-variable-name y))
  1431.                    (y-len (length y)))
  1432.               (declare (simple-string y))
  1433.               (and (>= y-len name-len)
  1434.                    (string= x y :end1 name-len :end2 name-len))))
  1435.           :end (or end (length variables)))))
  1436.  
  1437. ;;; DEBUG-FUNCTION-LAMBDA-LIST -- Public.
  1438. ;;;
  1439. (defun debug-function-lambda-list (debug-function)
  1440.   "Returns a list representing the lambda-list for debug-function.  The list
  1441.    has the following structure:
  1442.       (required-var1 required-var2
  1443.        ...
  1444.        (:optional var3 suppliedp-var4)
  1445.        (:optional var5)
  1446.        ...
  1447.        (:rest var6) (:rest var7)
  1448.        ...
  1449.        (:keyword keyword-symbol var8 suppliedp-var9)
  1450.        (:keyword keyword-symbol var10)
  1451.        ...
  1452.       )
  1453.    Each VARi is a debug-variable; however it may be the symbol :deleted it
  1454.    is unreferenced in debug-function.  This signals a lambda-list-unavaliable
  1455.    condition when there is no argument list information."
  1456.   (etypecase debug-function
  1457.     (compiled-debug-function
  1458.      (compiled-debug-function-lambda-list debug-function))
  1459.     (interpreted-debug-function
  1460.      (interpreted-debug-function-lambda-list debug-function))
  1461.     (bogus-debug-function
  1462.      nil)))
  1463.  
  1464. ;;; INTERPRETED-DEBUG-FUNCTION-LAMBDA-LIST -- Internal.
  1465. ;;; 
  1466. ;;; The hard part is when the lambda-list is unparsed.  If it is unparsed,
  1467. ;;; and all the arguments are required, this is still pretty easy; just
  1468. ;;; whip the appropriate debug-variables into a list.  Otherwise, we have
  1469. ;;; to pick out the funny arguments including any suppliedp variables.  In
  1470. ;;; this situation, the ir1-lambda is an external entry point that takes
  1471. ;;; arguments users really pass in.  It looks at those and computes defaults
  1472. ;;; and suppliedp variables, ultimately passing everything defined as a
  1473. ;;; a parameter to the real function as final arguments.  If this has to
  1474. ;;; compute the lambda list, it caches it in debug-function.
  1475. ;;;
  1476. (defun interpreted-debug-function-lambda-list (debug-function)
  1477.   (let ((lambda-list (debug-function-%lambda-list debug-function))
  1478.     (debug-vars (debug-function-debug-variables debug-function))
  1479.     (ir1-lambda (interpreted-debug-function-ir1-lambda debug-function))
  1480.     (res nil))
  1481.     (if (eq lambda-list :unparsed)
  1482.     (flet ((frob (v debug-vars)
  1483.          (if (c::lambda-var-refs v)
  1484.              (find v debug-vars
  1485.                :key #'interpreted-debug-variable-ir1-var)
  1486.              :deleted)))
  1487.       (let ((xep-args (c::lambda-optional-dispatch ir1-lambda)))
  1488.         (if (and xep-args
  1489.              (eq (c::optional-dispatch-main-entry xep-args) ir1-lambda))
  1490.         ;;
  1491.         ;; There are rest, optional, keyword, and suppliedp vars.
  1492.         (let ((final-args (c::lambda-vars ir1-lambda)))
  1493.           (dolist (xep-arg (c::optional-dispatch-arglist xep-args))
  1494.             (let ((info (c::lambda-var-arg-info xep-arg))
  1495.               (final-arg (pop final-args)))
  1496.               (cond (info
  1497.                  (case (c::arg-info-kind info)
  1498.                    (:required
  1499.                 (push (frob final-arg debug-vars) res))
  1500.                    (:keyword
  1501.                 (push (list :keyword
  1502.                         (c::arg-info-keyword info)
  1503.                         (frob final-arg debug-vars))
  1504.                       res))
  1505.                    (:rest
  1506.                 (push (list :rest (frob final-arg debug-vars))
  1507.                       res))
  1508.                    (:optional
  1509.                 (push (list :optional
  1510.                         (frob final-arg debug-vars))
  1511.                       res)))
  1512.                  (when (c::arg-info-supplied-p info)
  1513.                    (nconc
  1514.                 (car res)
  1515.                 (list (frob (pop final-args) debug-vars)))))
  1516.                 (t
  1517.                  (push (frob final-arg debug-vars) res)))))
  1518.           (setf (debug-function-%lambda-list debug-function)
  1519.             (nreverse res)))
  1520.         ;;
  1521.         ;; All required args, so return them in a list.
  1522.         (dolist (v (c::lambda-vars ir1-lambda)
  1523.                (setf (debug-function-%lambda-list debug-function)
  1524.                  (nreverse res)))
  1525.           (push (frob v debug-vars) res)))))
  1526.     ;;
  1527.     ;; Everything's unparsed and cached, so return it.
  1528.     lambda-list)))
  1529.  
  1530. ;;; COMPILED-DEBUG-FUNCTION-LAMBDA-LIST -- Internal.
  1531. ;;;
  1532. ;;; If this has to compute the lambda list, it caches it in debug-function.
  1533. ;;;
  1534. (defun compiled-debug-function-lambda-list (debug-function)
  1535.   (let ((lambda-list (debug-function-%lambda-list debug-function)))
  1536.     (cond ((eq lambda-list :unparsed)
  1537.        (multiple-value-bind
  1538.            (args argsp)
  1539.            (parse-compiled-debug-function-lambda-list debug-function)
  1540.          (setf (debug-function-%lambda-list debug-function) args)
  1541.          (if argsp
  1542.          args
  1543.          (debug-signal 'lambda-list-unavailable
  1544.                    :debug-function debug-function))))
  1545.       (lambda-list)
  1546.       ((bogus-debug-function-p debug-function)
  1547.        nil)
  1548.       ((c::compiled-debug-function-arguments
  1549.         (compiled-debug-function-compiler-debug-fun
  1550.          debug-function))
  1551.        ;; If the packed information is there (whether empty or not) as
  1552.        ;; opposed to being nil, then returned our cached value (nil).
  1553.        nil)
  1554.       (t
  1555.        ;; Our cached value is nil, and the packed lambda-list information
  1556.        ;; is nil, so we don't have anything available.
  1557.        (debug-signal 'lambda-list-unavailable
  1558.              :debug-function debug-function)))))
  1559.  
  1560. ;;; PARSE-COMPILED-DEBUG-FUNCTION-LAMBDA-LIST -- Internal.
  1561. ;;;
  1562. ;;; COMPILED-DEBUG-FUNCTION-LAMBDA-LIST calls this when a
  1563. ;;; compiled-debug-function has no lambda-list information cached.  It returns
  1564. ;;; the lambda-list as the first value and whether there was any argument
  1565. ;;; information as the second value.  Therefore, nil and t means there were no
  1566. ;;; arguments, but nil and nil means there was no argument information.
  1567. ;;;
  1568. (defun parse-compiled-debug-function-lambda-list (debug-function)
  1569.   (let ((args (c::compiled-debug-function-arguments
  1570.            (compiled-debug-function-compiler-debug-fun
  1571.         debug-function))))
  1572.     (cond
  1573.      ((not args)
  1574.       (values nil nil))
  1575.      ((eq args :minimal)
  1576.       (values (coerce (debug-function-debug-variables debug-function) 'list)
  1577.           t))
  1578.      (t
  1579.       (let ((vars (debug-function-debug-variables debug-function))
  1580.         (i 0)
  1581.         (len (length args))
  1582.         (res nil)
  1583.         (optionalp nil))
  1584.     (declare (type (or null simple-vector) vars))
  1585.     (loop
  1586.       (when (>= i len) (return))
  1587.       (let ((ele (aref args i)))
  1588.         (cond
  1589.          ((symbolp ele)
  1590.           (case ele
  1591.         (c::deleted
  1592.          ;; Deleted required arg at beginning of args array.
  1593.          (push :deleted res))
  1594.         (c::optional-args
  1595.          (setf optionalp t))
  1596.         (c::supplied-p
  1597.          ;; supplied-p var immediately following keyword or optional.
  1598.          ;; Stick the extra var in the result element representing
  1599.          ;; the keyword or optional, which is the previous one.
  1600.          (nconc (car res)
  1601.             (list (compiled-debug-function-lambda-list-var
  1602.                    args (incf i) vars))))
  1603.         (c::rest-arg
  1604.          (push (list :rest
  1605.                  (compiled-debug-function-lambda-list-var
  1606.                   args (incf i) vars))
  1607.                res))
  1608.         (c::more-arg
  1609.          (error "I thought I'd never see a more-arg?"))
  1610.         (t
  1611.          ;; Keyword arg.
  1612.          (push (list :keyword
  1613.                  ele
  1614.                  (compiled-debug-function-lambda-list-var
  1615.                   args (incf i) vars))
  1616.                res))))
  1617.          (optionalp
  1618.           ;; We saw an optional marker, so the following non-symbols are
  1619.           ;; indexes indicating optional variables.
  1620.           (push (list :optional (svref vars ele)) res))
  1621.          (t
  1622.           ;; Required arg at beginning of args array.
  1623.           (push (svref vars ele) res))))
  1624.       (incf i))
  1625.     (values (nreverse res) t))))))
  1626.  
  1627. ;;; COMPILED-DEBUG-FUNCTION-LAMBDA-LIST-VAR -- Internal
  1628. ;;;
  1629. ;;; Used in COMPILED-DEBUG-FUNCTION-LAMBDA-LIST.
  1630. ;;;
  1631. (defun compiled-debug-function-lambda-list-var (args i vars)
  1632.   (declare (type (simple-array * (*)) args)
  1633.        (simple-vector vars))
  1634.   (let ((ele (aref args i)))
  1635.     (cond ((not (symbolp ele)) (svref vars ele))
  1636.       ((eq ele 'c::deleted) :deleted)
  1637.       (t (error "Malformed arguments description.")))))
  1638.  
  1639. ;;; COMPILED-DEBUG-FUNCTION-DEBUG-INFO -- Internal.
  1640. ;;;
  1641. (defun compiled-debug-function-debug-info (debug-fun)
  1642.   (kernel:code-debug-info (compiled-debug-function-component debug-fun)))
  1643.  
  1644.  
  1645.  
  1646. ;;;; Unpacking variable and basic block data.
  1647.  
  1648. (defvar *parsing-buffer*
  1649.   (make-array 20 :adjustable t :fill-pointer t))
  1650. (defvar *other-parsing-buffer*
  1651.   (make-array 20 :adjustable t :fill-pointer t))
  1652. ;;;
  1653. ;;; WITH-PARSING-BUFFER -- Internal.
  1654. ;;;
  1655. ;;; PARSE-DEBUG-BLOCKS, PARSE-DEBUG-VARIABLES and UNCOMPACT-FUNCTION-MAP use
  1656. ;;; this to unpack binary encoded information.  It returns the values returned
  1657. ;;; by the last form in body.
  1658. ;;;
  1659. ;;; This binds buffer-var to *parsing-buffer*, makes sure it starts at element
  1660. ;;; zero, and makes sure if we unwind, we nil out any set elements for GC
  1661. ;;; purposes.
  1662. ;;;
  1663. ;;; This also binds other-var to *other-parsing-buffer* when it is supplied,
  1664. ;;; making sure it starts at element zero and that we nil out any elements if
  1665. ;;; we unwind.
  1666. ;;;
  1667. ;;; This defines the local macro RESULT that takes a buffer, copies its
  1668. ;;; elements to a resulting simple-vector, nil's out elements, and restarts
  1669. ;;; the buffer at element zero.  RESULT returns the simple-vector.
  1670. ;;;
  1671. (eval-when (compile eval)
  1672. (defmacro with-parsing-buffer ((buffer-var &optional other-var) &body body)
  1673.   (let ((len (gensym))
  1674.     (res (gensym)))
  1675.     `(unwind-protect
  1676.      (let ((,buffer-var *parsing-buffer*)
  1677.            ,@(if other-var `((,other-var *other-parsing-buffer*))))
  1678.        (setf (fill-pointer ,buffer-var) 0)
  1679.        ,@(if other-var `((setf (fill-pointer ,other-var) 0)))
  1680.        (macrolet ((result (buf)
  1681.             `(let* ((,',len (length ,buf))
  1682.                 (,',res (make-array ,',len)))
  1683.                (replace ,',res ,buf :end1 ,',len :end2 ,',len)
  1684.                (fill ,buf nil :end ,',len)
  1685.                (setf (fill-pointer ,buf) 0)
  1686.                ,',res)))
  1687.          ,@body))
  1688.      (fill *parsing-buffer* nil)
  1689.      ,@(if other-var `((fill *other-parsing-buffer* nil))))))
  1690. ) ;eval-when
  1691.  
  1692.  
  1693. ;;; DEBUG-FUNCTION-DEBUG-BLOCKS -- Internal.
  1694. ;;;
  1695. ;;; The argument is a debug internals structure.  This returns the debug-blocks
  1696. ;;; for debug-function, regardless of whether we have unpacked them yet.  It
  1697. ;;; signals a no-debug-blocks condition if it can't return the blocks.
  1698. ;;;
  1699. (defun debug-function-debug-blocks (debug-function)
  1700.   (let ((blocks (debug-function-blocks debug-function)))
  1701.     (cond ((eq blocks :unparsed)
  1702.        (setf (debug-function-blocks debug-function)
  1703.          (parse-debug-blocks debug-function))
  1704.        (unless (debug-function-blocks debug-function)
  1705.          (debug-signal 'no-debug-blocks
  1706.                :debug-function debug-function))
  1707.        (debug-function-blocks debug-function))
  1708.       (blocks)
  1709.       (t
  1710.        (debug-signal 'no-debug-blocks
  1711.              :debug-function debug-function)))))
  1712.  
  1713. ;;; PARSE-DEBUG-BLOCKS -- Internal.
  1714. ;;;
  1715. ;;; This returns a simple-vector of debug-blocks or nil.  Nil indicates there
  1716. ;;; was no basic block information.
  1717. ;;;
  1718. (defun parse-debug-blocks (debug-function)
  1719.   (etypecase debug-function
  1720.     (compiled-debug-function
  1721.      (parse-compiled-debug-blocks debug-function))
  1722.     (bogus-debug-function
  1723.      (debug-signal 'no-debug-blocks :debug-function debug-function))
  1724.     (interpreted-debug-function
  1725.      (parse-interpreted-debug-blocks debug-function))))
  1726.  
  1727.  
  1728. ;;; PARSE-COMPILED-DEBUG-BLOCKS -- Internal.
  1729. ;;;
  1730. ;;; This does some of the work of PARSE-DEBUG-BLOCKS.
  1731. ;;;
  1732. (defun parse-compiled-debug-blocks (debug-function)
  1733.   (let* ((debug-fun (compiled-debug-function-compiler-debug-fun debug-function))
  1734.      (var-count (length (debug-function-debug-variables debug-function)))
  1735.      (blocks (c::compiled-debug-function-blocks debug-fun))
  1736.      ;; 8 is a hard-wired constant in the compiler for the element size of
  1737.      ;; the packed binary representation of the blocks data.
  1738.      (live-set-len (ceiling var-count 8))
  1739.      (tlf-number (c::compiled-debug-function-tlf-number debug-fun)))
  1740.     (unless blocks (return-from parse-compiled-debug-blocks nil))
  1741.     (macrolet ((aref+ (a i) `(prog1 (aref ,a ,i) (incf ,i))))
  1742.       (with-parsing-buffer (blocks-buffer locations-buffer)
  1743.     (let ((i 0)
  1744.           (len (length blocks))
  1745.           (last-pc 0))
  1746.       (loop
  1747.         (when (>= i len) (return))
  1748.         (let ((succ-and-flags (aref+ blocks i))
  1749.           (successors nil))
  1750.           (declare (type (unsigned-byte 8) succ-and-flags)
  1751.                (list successors))
  1752.           (dotimes (k (ldb c::compiled-debug-block-nsucc-byte
  1753.                    succ-and-flags))
  1754.         (push (c::read-var-integer blocks i) successors))
  1755.           (let* ((locations
  1756.               (dotimes (k (c::read-var-integer blocks i)
  1757.                   (result locations-buffer))
  1758.             (let ((kind (svref c::compiled-code-location-kinds
  1759.                        (aref+ blocks i)))
  1760.                   (pc (+ last-pc (c::read-var-integer blocks i)))
  1761.                   (tlf-offset (or tlf-number
  1762.                           (c::read-var-integer blocks i)))
  1763.                   (form-number (c::read-var-integer blocks i))
  1764.                   (live-set (c::read-packed-bit-vector
  1765.                      live-set-len blocks i)))
  1766.               (vector-push-extend (make-known-code-location
  1767.                            pc debug-function tlf-offset
  1768.                            form-number live-set kind)
  1769.                           locations-buffer)
  1770.               (setf last-pc pc))))
  1771.              (block (make-compiled-debug-block
  1772.                  locations successors
  1773.                  (not (zerop (logand
  1774.                       c::compiled-debug-block-elsewhere-p
  1775.                       succ-and-flags))))))
  1776.         (vector-push-extend block blocks-buffer)
  1777.         (dotimes (k (length locations))
  1778.           (setf (code-location-%debug-block (svref locations k))
  1779.             block))))))
  1780.     (let ((res (result blocks-buffer)))
  1781.       (declare (simple-vector res))
  1782.       (dotimes (i (length res))
  1783.         (let* ((block (svref res i))
  1784.            (succs nil))
  1785.           (dolist (ele (debug-block-successors block))
  1786.         (push (svref res ele) succs))
  1787.           (setf (debug-block-successors block) succs)))
  1788.       res)))))
  1789.  
  1790. ;;; PARSE-INTERPRETED-DEBUG-BLOCKS -- Internal.
  1791. ;;;
  1792. ;;; This does some of the work of PARSE-DEBUG-BLOCKS.
  1793. ;;;
  1794. (defun parse-interpreted-debug-blocks (debug-function)
  1795.   (let ((ir1-lambda (interpreted-debug-function-ir1-lambda debug-function)))
  1796.     (with-parsing-buffer (buffer)
  1797.       (c::do-blocks (block (c::block-component
  1798.                 (c::node-block (c::lambda-bind ir1-lambda))))
  1799.     (when (eq ir1-lambda (c::block-home-lambda block))
  1800.       (vector-push-extend (make-interpreted-debug-block block) buffer)))
  1801.       (result buffer))))
  1802.  
  1803.  
  1804. ;;; DEBUG-FUNCTION-DEBUG-VARIABLES -- Internal.
  1805. ;;;
  1806. ;;; The argument is a debug internals structure.  This returns nil if there is
  1807. ;;; no variable information.  It returns an empty simple-vector if there were
  1808. ;;; no locals in the function.  Otherwise it returns a simple-vector of
  1809. ;;; debug-variables.
  1810. ;;;
  1811. (defun debug-function-debug-variables (debug-function)
  1812.   (let ((vars (debug-function-debug-vars debug-function)))
  1813.     (if (eq vars :unparsed)
  1814.     (setf (debug-function-debug-vars debug-function)
  1815.           (etypecase debug-function
  1816.         (compiled-debug-function
  1817.          (parse-compiled-debug-variables debug-function))
  1818.         (bogus-debug-function nil)
  1819.         (interpreted-debug-function
  1820.          (parse-interpreted-debug-variables debug-function))))
  1821.     vars)))
  1822.  
  1823.  
  1824. ;;; PARSE-INTERPRETED-DEBUG-VARIABLES -- Internal.
  1825. ;;;
  1826. ;;; This grabs all the variables from debug-fun's ir1-lambda, from the IR1
  1827. ;;; lambda vars, and all of it's LET's.  Each LET is an IR1 lambda.  For each
  1828. ;;; variable, we make an interpreted-debug-variable.  We then SORT all the
  1829. ;;; variables by name.  Then we go through, and for any duplicated names we
  1830. ;;; distinguish the interpreted-debug-variables by setting their id slots to a
  1831. ;;; distinct number.
  1832. ;;;
  1833. (defun parse-interpreted-debug-variables (debug-fun)
  1834.   (let* ((ir1-lambda (interpreted-debug-function-ir1-lambda debug-fun))
  1835.      (vars (flet ((frob (ir1-lambda buf)
  1836.             (dolist (v (c::lambda-vars ir1-lambda))
  1837.               (vector-push-extend
  1838.                (let* ((id (c::leaf-name v))
  1839.                   (pkg (symbol-package id)))
  1840.                  (make-interpreted-debug-variable
  1841.                   (symbol-name id)
  1842.                   (when pkg (package-name pkg))
  1843.                   v))
  1844.                buf))))
  1845.          (with-parsing-buffer (buf)
  1846.            (frob ir1-lambda buf)
  1847.            (dolist (let-lambda (c::lambda-lets ir1-lambda))
  1848.              (frob let-lambda buf))
  1849.            (result buf)))))
  1850.     (declare (simple-vector vars))
  1851.     (sort vars #'string< :key #'debug-variable-name)
  1852.     (let ((len (length vars)))
  1853.       (when (> len 1)
  1854.     (let ((i 0)
  1855.           (j 1))
  1856.       (block PUNT
  1857.         (loop
  1858.           (let* ((var-i (svref vars i))
  1859.              (var-j (svref vars j))
  1860.              (name (debug-variable-name var-i)))
  1861.         (when (string= name (debug-variable-name var-j))
  1862.           (let ((count 1))
  1863.             (loop 
  1864.               (setf (debug-variable-id var-j) count)
  1865.               (when (= (incf j) len) (return-from PUNT))
  1866.               (setf var-j (svref vars j))
  1867.               (when (string/= name (debug-variable-name var-j))
  1868.             (return))
  1869.               (incf count))))
  1870.         (setf i j)
  1871.         (incf j)
  1872.         (when (= j len) (return))))))))
  1873.     vars))
  1874.  
  1875.  
  1876. ;;; ASSIGN-MINIMAL-VAR-NAMES -- Internal.
  1877. ;;;
  1878. ;;; Vars is the parsed variables for a minimal debug function.  We need to
  1879. ;;; assign names of the form ARG-NNN.  We must pad with leading zeros, since
  1880. ;;; the arguments must be in alphabetical order.
  1881. ;;;
  1882. (defun assign-minimal-var-names (vars)
  1883.   (declare (simple-vector vars))
  1884.   (let* ((len (length vars))
  1885.      (width (length (format nil "~D" (1- len)))))
  1886.     (dotimes (i len)
  1887.       (setf (compiled-debug-variable-name (svref vars i))
  1888.         (format nil "ARG-~V,'0D" width i)))))
  1889.  
  1890.   
  1891. ;;; PARSE-COMPILED-DEBUG-VARIABLES -- Internal.
  1892. ;;;
  1893. ;;; This parses the packed binary representation of debug-variables from
  1894. ;;; debug-function's c::compiled-debug-function.
  1895. ;;;
  1896. (defun parse-compiled-debug-variables (debug-function)
  1897.   (let* ((debug-fun (compiled-debug-function-compiler-debug-fun debug-function))
  1898.      (packed-vars (c::compiled-debug-function-variables debug-fun))
  1899.      (default-package (c::compiled-debug-info-package
  1900.                (compiled-debug-function-debug-info debug-function)))
  1901.      (args-minimal (eq (c::compiled-debug-function-arguments debug-fun)
  1902.                :minimal)))
  1903.     (unless packed-vars
  1904.       (return-from parse-compiled-debug-variables nil))
  1905.     (when (zerop (length packed-vars))
  1906.       ;; Return a simple-vector not whatever packed-vars may be.
  1907.       (return-from parse-compiled-debug-variables '#()))
  1908.     (let ((i 0)
  1909.       (len (length packed-vars)))
  1910.       (with-parsing-buffer (buffer)
  1911.     (loop
  1912.       ;; The routines in the "C" package are macros that advance the
  1913.       ;; index.
  1914.       (let* ((flags (prog1 (aref packed-vars i) (incf i)))
  1915.          (minimal (logtest c::compiled-debug-variable-minimal-p flags))
  1916.          (deleted (logtest c::compiled-debug-variable-deleted-p flags))
  1917.          (name (if minimal "" (c::read-var-string packed-vars i)))
  1918.          (package (cond
  1919.                (minimal default-package)
  1920.                ((logtest c::compiled-debug-variable-packaged
  1921.                      flags)
  1922.                 (c::read-var-string packed-vars i))
  1923.                ((logtest c::compiled-debug-variable-uninterned
  1924.                      flags)
  1925.                 nil)
  1926.                (t
  1927.                 default-package)))
  1928.           (id (if (logtest c::compiled-debug-variable-id-p flags)
  1929.               (c::read-var-integer packed-vars i)
  1930.               0))
  1931.           (sc-offset
  1932.            (if deleted 0 (c::read-var-integer packed-vars i)))
  1933.           (save-sc-offset
  1934.            (if (logtest c::compiled-debug-variable-save-loc-p flags)
  1935.                (c::read-var-integer packed-vars i)
  1936.                nil)))
  1937.         (assert (not (and args-minimal (not minimal))))
  1938.         (vector-push-extend
  1939.          (make-compiled-debug-variable
  1940.           name package id
  1941.           (logtest c::compiled-debug-variable-environment-live flags)
  1942.           sc-offset save-sc-offset)
  1943.          buffer))
  1944.       (when (>= i len) (return)))
  1945.     (let ((res (result buffer)))
  1946.       (when args-minimal
  1947.         (assign-minimal-var-names res))
  1948.       res)))))
  1949.  
  1950.  
  1951. ;;;; Unpacking minimal debug functions.
  1952.  
  1953. (eval-when (compile eval)
  1954.  
  1955. ;;; MAKE-UNCOMPACTED-DEBUG-FUN -- Internal.
  1956. ;;;
  1957. ;;; Sleazoid "macro" to keep our indentation sane in UNCOMPACT-FUNCTION-MAP.
  1958. ;;;
  1959. (defmacro make-uncompacted-debug-fun ()
  1960.   '(c::make-compiled-debug-function
  1961.     :name
  1962.     (let ((base (ecase (ldb c::minimal-debug-function-name-style-byte
  1963.                 options)
  1964.           (#.c::minimal-debug-function-name-symbol
  1965.            (intern (c::read-var-string map i)
  1966.                (c::compiled-debug-info-package info)))
  1967.           (#.c::minimal-debug-function-name-packaged
  1968.            (let ((pkg (c::read-var-string map i)))
  1969.              (intern (c::read-var-string map i) pkg)))
  1970.           (#.c::minimal-debug-function-name-uninterned
  1971.            (make-symbol (c::read-var-string map i)))
  1972.           (#.c::minimal-debug-function-name-component
  1973.            (c::compiled-debug-info-name info)))))
  1974.       (if (logtest flags c::minimal-debug-function-setf-bit)
  1975.       `(setf ,base)
  1976.       base))
  1977.     :kind (svref c::minimal-debug-function-kinds
  1978.          (ldb c::minimal-debug-function-kind-byte options))
  1979.     :variables
  1980.     (when vars-p
  1981.       (let ((len (c::read-var-integer map i)))
  1982.     (prog1 (subseq map i (+ i len))
  1983.       (incf i len))))
  1984.     :arguments (when vars-p :minimal)
  1985.     :returns
  1986.     (ecase (ldb c::minimal-debug-function-returns-byte options)
  1987.       (#.c::minimal-debug-function-returns-standard
  1988.        :standard)
  1989.       (#.c::minimal-debug-function-returns-fixed
  1990.        :fixed)
  1991.       (#.c::minimal-debug-function-returns-specified
  1992.        (with-parsing-buffer (buf)
  1993.      (dotimes (idx (c::read-var-integer map i))
  1994.        (vector-push-extend (c::read-var-integer map i) buf))
  1995.      (result buf))))
  1996.     :return-pc (c::read-var-integer map i)
  1997.     :old-fp (c::read-var-integer map i)
  1998.     :nfp (when (logtest flags c::minimal-debug-function-nfp-bit)
  1999.        (c::read-var-integer map i))
  2000.     :start-pc
  2001.     (progn
  2002.       (setq code-start-pc (+ code-start-pc (c::read-var-integer map i)))
  2003.       (+ code-start-pc (c::read-var-integer map i)))
  2004.     :elsewhere-pc
  2005.     (setq elsewhere-pc (+ elsewhere-pc (c::read-var-integer map i)))))
  2006.  
  2007. ) ;EVAL-WHEN (compile eval)
  2008.  
  2009. ;;; UNCOMPACT-FUNCTION-MAP  --  Internal
  2010. ;;;
  2011. ;;;    Return a normal function map derived from a minimal debug info function
  2012. ;;; map.  This involves looping parsing minimal-debug-functions and then
  2013. ;;; building a vector out of them.
  2014. ;;;
  2015. (defun uncompact-function-map (info)
  2016.   (declare (type c::compiled-debug-info info))
  2017.   (let* ((map (c::compiled-debug-info-function-map info))
  2018.      (i 0)
  2019.      (len (length map))
  2020.      (code-start-pc 0)
  2021.      (elsewhere-pc 0))
  2022.     (declare (type (simple-array (unsigned-byte 8) (*)) map))
  2023.     (ext:collect ((res))
  2024.       (loop
  2025.     (when (= i len) (return))
  2026.     (let* ((options (prog1 (aref map i) (incf i)))
  2027.            (flags (prog1 (aref map i) (incf i)))
  2028.            (vars-p (logtest flags c::minimal-debug-function-variables-bit))
  2029.            (dfun (make-uncompacted-debug-fun)))
  2030.       (res code-start-pc)
  2031.       (res dfun)))
  2032.       
  2033.       (coerce (cdr (res)) 'simple-vector))))
  2034.  
  2035.     
  2036. ;;; This variable maps minimal debug-info function maps to an unpacked version
  2037. ;;; thereof.
  2038. ;;;
  2039. (defvar *uncompacted-function-maps* (make-hash-table :test #'eq))
  2040.  
  2041. ;;; GET-DEBUG-INFO-FUNCTION-MAP  --  Internal
  2042. ;;;
  2043. ;;;    Return a function-map for a given compiled-debug-info object.  If the
  2044. ;;; info is minimal, and has not been parsed, then parse it.
  2045. ;;;
  2046. (defun get-debug-info-function-map (info)
  2047.   (declare (type c::compiled-debug-info info))
  2048.   (let ((map (c::compiled-debug-info-function-map info)))
  2049.     (if (simple-vector-p map)
  2050.     map
  2051.     (or (gethash map *uncompacted-function-maps*)
  2052.         (setf (gethash map *uncompacted-function-maps*)
  2053.           (uncompact-function-map info))))))
  2054.  
  2055.  
  2056. ;;;; Code-locations.
  2057.  
  2058. ;;; CODE-LOCATION-UNKNOWN-P -- Public.
  2059. ;;;
  2060. ;;; If we're sure of whether code-location is known, return t or nil.  If we're
  2061. ;;; :unsure, then try to fill in the code-location's slots.  This determines
  2062. ;;; whether there is any debug-block information, and if code-location is
  2063. ;;; known.
  2064. ;;;
  2065. ;;; ??? IF this conses closures every time it's called, then break off the
  2066. ;;; :unsure part to get the HANDLER-CASE into another function.
  2067. ;;;
  2068. (defun code-location-unknown-p (basic-code-location)
  2069.   "Returns whether basic-code-location is unknown.  It returns nil when the
  2070.    code-location is known."
  2071.   (ecase (code-location-%unknown-p basic-code-location)
  2072.     ((t) t)
  2073.     ((nil) nil)
  2074.     (:unsure
  2075.      (setf (code-location-%unknown-p basic-code-location)
  2076.        (handler-case (not (fill-in-code-location basic-code-location))
  2077.          (no-debug-blocks () t))))))
  2078.  
  2079. ;;; CODE-LOCATION-DEBUG-BLOCK -- Public.
  2080. ;;;
  2081. (defun code-location-debug-block (basic-code-location)
  2082.   "Returns the debug-block containing code-location if it is available.  Some
  2083.    debug policies inhibit debug-block information, and if none is available,
  2084.    then this signals a no-debug-blocks condition."
  2085.   (let ((block (code-location-%debug-block basic-code-location)))
  2086.     (if (eq block :unparsed)
  2087.     (etypecase basic-code-location
  2088.       (compiled-code-location
  2089.        (compute-compiled-code-location-debug-block basic-code-location))
  2090.       (interpreted-code-location
  2091.        (setf (code-location-%debug-block basic-code-location)
  2092.          (make-interpreted-debug-block
  2093.           (c::node-block
  2094.            (interpreted-code-location-ir1-node basic-code-location))))))
  2095.     block)))
  2096.  
  2097. ;;; COMPUTE-COMPILED-CODE-LOCATION-DEBUG-BLOCK -- Internal.
  2098. ;;;
  2099. ;;; This stores and returns basic-code-location's debug-block.  It determines
  2100. ;;; the correct one using the code-location's pc.  This uses
  2101. ;;; DEBUG-FUNCTION-DEBUG-BLOCKS to return the cached block information or
  2102. ;;; signal a 'no-debug-blocks condition.  The blocks are sorted by their first
  2103. ;;; code-location's pc, in ascending order.  Therefore, as soon as we find a
  2104. ;;; block that starts with a pc greater than basic-code-location's pc, we know
  2105. ;;; the previous block contains the pc.  If we get to the last block, then the
  2106. ;;; code-location is either in the second to last block or the last block, and
  2107. ;;; we have to be careful in determining this since the last block could be
  2108. ;;; random code at the end of the function.  We have to check for the last
  2109. ;;; block being random code first to see how to compare the code-location's pc.
  2110. ;;;
  2111. (defun compute-compiled-code-location-debug-block (basic-code-location)
  2112.   (let* ((pc (compiled-code-location-pc basic-code-location))
  2113.      (debug-function (code-location-debug-function
  2114.               basic-code-location))
  2115.      (blocks (debug-function-debug-blocks debug-function))
  2116.      (len (length blocks)))
  2117.     (declare (simple-vector blocks))
  2118.     (setf (code-location-%debug-block basic-code-location)
  2119.       (if (= len 1)
  2120.           (svref blocks 0)
  2121.           (do ((i 1 (1+ i))
  2122.            (end (1- len)))
  2123.           ((= i end)
  2124.            (let ((last (svref blocks end)))
  2125.              (cond
  2126.               ((debug-block-elsewhere-p last)
  2127.                (if (< pc
  2128.                   (c::compiled-debug-function-elsewhere-pc
  2129.                    (compiled-debug-function-compiler-debug-fun
  2130.                 debug-function)))
  2131.                (svref blocks (1- end))
  2132.                last))
  2133.               ((< pc
  2134.               (compiled-code-location-pc
  2135.                (svref (compiled-debug-block-code-locations last)
  2136.                   0)))
  2137.                (svref blocks (1- end)))
  2138.               (t last))))
  2139.         (declare (type c::index i end))
  2140.         (when (< pc
  2141.              (compiled-code-location-pc
  2142.               (svref (compiled-debug-block-code-locations
  2143.                   (svref blocks i))
  2144.                  0)))
  2145.           (return (svref blocks (1- i)))))))))
  2146.  
  2147. ;;; CODE-LOCATION-DEBUG-SOURCE -- Public.
  2148. ;;;
  2149. (defun code-location-debug-source (code-location)
  2150.   "Returns the code-location's debug-source."
  2151.   (etypecase code-location
  2152.     (compiled-code-location
  2153.      (let* ((info (compiled-debug-function-debug-info
  2154.            (code-location-debug-function code-location)))
  2155.         (sources (c::compiled-debug-info-source info))
  2156.         (len (length sources)))
  2157.      (declare (list sources))
  2158.      (if (= len 1)
  2159.          (car sources)
  2160.          (do ((prev sources src)
  2161.           (src (cdr sources) (cdr src))
  2162.           (offset (code-location-top-level-form-offset code-location)))
  2163.          ((null src) (car prev))
  2164.            (when (< offset (c::debug-source-source-root (car src)))
  2165.          (return (car prev)))))))
  2166.     (interpreted-code-location
  2167.      (first
  2168.       (let ((c::*lexical-environment* (c::make-null-environment)))
  2169.     (c::debug-source-for-info
  2170.      (c::component-source-info
  2171.       (c::block-component
  2172.        (c::node-block
  2173.         (interpreted-code-location-ir1-node code-location))))))))))
  2174.  
  2175.  
  2176. ;;; CODE-LOCATION-TOP-LEVEL-FORM-OFFSET -- Public.
  2177. ;;;
  2178. (defun code-location-top-level-form-offset (code-location)
  2179.   "Returns the number of top-level forms before the one containing
  2180.    code-location as seen by the compiler in some compilation unit.  A
  2181.    compilation unit is not necessarily a single file, see the section on
  2182.    debug-sources."
  2183.   (when (code-location-unknown-p code-location)
  2184.     (error 'unknown-code-location :code-location code-location))
  2185.   (let ((tlf-offset (code-location-%tlf-offset code-location)))
  2186.     (cond ((eq tlf-offset :unparsed)
  2187.        (etypecase code-location
  2188.          (compiled-code-location
  2189.           (unless (fill-in-code-location code-location)
  2190.         ;; This check should be unnecessary.  We're missing debug info
  2191.         ;; the compiler should have dumped.
  2192.         (error "Unknown code location?  It should be known."))
  2193.           (code-location-%tlf-offset code-location))
  2194.          (interpreted-code-location
  2195.           (setf (code-location-%tlf-offset code-location)
  2196.             (c::source-path-tlf-number
  2197.              (c::node-source-path
  2198.               (interpreted-code-location-ir1-node code-location)))))))
  2199.       (t tlf-offset))))
  2200.  
  2201. ;;; CODE-LOCATION-FORM-NUMBER -- Public.
  2202. ;;;
  2203. (defun code-location-form-number (code-location)
  2204.   "Returns the number of the form corresponding to code-location.  The form
  2205.    number is derived by a walking the subforms of a top-level form in
  2206.    depth-first order."
  2207.   (when (code-location-unknown-p code-location)
  2208.     (error 'unknown-code-location :code-location code-location))
  2209.   (let ((form-num (code-location-%form-number code-location)))
  2210.     (cond ((eq form-num :unparsed)
  2211.        (etypecase code-location
  2212.          (compiled-code-location
  2213.           (unless (fill-in-code-location code-location)
  2214.         ;; This check should be unnecessary.  We're missing debug info
  2215.         ;; the compiler should have dumped.
  2216.         (error "Unknown code location?  It should be known."))
  2217.           (code-location-%form-number code-location))
  2218.          (interpreted-code-location
  2219.           (setf (code-location-%form-number code-location)
  2220.             (c::source-path-form-number
  2221.              (c::node-source-path
  2222.               (interpreted-code-location-ir1-node code-location)))))))
  2223.       (t form-num))))
  2224.  
  2225. ;;; CODE-LOCATION-KIND -- Public
  2226. ;;; 
  2227. (defun code-location-kind (code-location)
  2228.   "Return the kind of CODE-LOCATION, one of:
  2229.      :interpreted, :unknown-return, :known-return, :internal-error,
  2230.      :non-local-exit, :block-start, :call-site, :single-value-return,
  2231.      :non-local-entry"
  2232.   (when (code-location-unknown-p code-location)
  2233.     (error 'unknown-code-location :code-location code-location))
  2234.   (etypecase code-location
  2235.     (compiled-code-location
  2236.      (let ((kind (compiled-code-location-kind code-location)))
  2237.        (cond ((not (eq kind :unparsed)) kind)
  2238.              ((not (fill-in-code-location code-location))
  2239.               ;; This check should be unnecessary.  We're missing
  2240.               ;; debug info the compiler should have dumped.
  2241.               (error "Unknown code location?  It should be known."))
  2242.              (t
  2243.               (compiled-code-location-kind code-location)))))
  2244.     (interpreted-code-location
  2245.      :interpreted)))
  2246.  
  2247.  
  2248. ;;; COMPILED-CODE-LOCATION-LIVE-SET -- Internal.
  2249. ;;;
  2250. ;;; This returns the code-location's live-set if it is available.  If there
  2251. ;;; is no debug-block information, this returns nil.
  2252. ;;;
  2253. (defun compiled-code-location-live-set (code-location)
  2254.   (if (code-location-unknown-p code-location)
  2255.       nil
  2256.       (let ((live-set (compiled-code-location-%live-set code-location)))
  2257.     (cond ((eq live-set :unparsed)
  2258.            (unless (fill-in-code-location code-location)
  2259.          ;; This check should be unnecessary.  We're missing debug info
  2260.          ;; the compiler should have dumped.
  2261.          (error "Unknown code location?  It should be known."))
  2262.            (compiled-code-location-%live-set code-location))
  2263.           (t live-set)))))
  2264.  
  2265. ;;; CODE-LOCATION= -- Public.
  2266. ;;;
  2267. (defun code-location= (obj1 obj2)
  2268.   "Returns whether obj1 and obj2 are the same place in the code."
  2269.   (etypecase obj1
  2270.     (compiled-code-location
  2271.      (etypecase obj2
  2272.        (compiled-code-location
  2273.     (and (eq (code-location-debug-function obj1)
  2274.          (code-location-debug-function obj2))
  2275.          (sub-compiled-code-location= obj1 obj2)))
  2276.        (interpreted-code-location
  2277.     nil)))
  2278.     (interpreted-code-location
  2279.      (etypecase obj2
  2280.        (compiled-code-location
  2281.     nil)
  2282.        (interpreted-code-location
  2283.     (eq (interpreted-code-location-ir1-node obj1)
  2284.         (interpreted-code-location-ir1-node obj2)))))))
  2285. ;;;
  2286. (defun sub-compiled-code-location= (obj1 obj2)
  2287.   (= (compiled-code-location-pc obj1)
  2288.      (compiled-code-location-pc obj2)))
  2289.  
  2290. ;;; FILL-IN-CODE-LOCATION -- Internal.
  2291. ;;;
  2292. ;;; This fills in location's :unparsed slots.  It returns t or nil depending on
  2293. ;;; whether the code-location was known in its debug-function's debug-block
  2294. ;;; information.  This may signal a no-debug-blocks condition due to
  2295. ;;; DEBUG-FUNCTION-DEBUG-BLOCKS, and it assumes the %unknown-p slot is already
  2296. ;;; set or going to be set.
  2297. ;;;
  2298. (defun fill-in-code-location (code-location)
  2299.   (declare (type compiled-code-location code-location))
  2300.   (let* ((debug-function (code-location-debug-function code-location))
  2301.      (blocks (debug-function-debug-blocks debug-function)))
  2302.     (declare (simple-vector blocks))
  2303.     (dotimes (i (length blocks) nil)
  2304.       (let* ((block (svref blocks i))
  2305.          (locations (compiled-debug-block-code-locations block)))
  2306.     (declare (simple-vector locations))
  2307.     (dotimes (j (length locations))
  2308.       (let ((loc (svref locations j)))
  2309.         (when (sub-compiled-code-location= code-location loc)
  2310.           (setf (code-location-%debug-block code-location) block)
  2311.           (setf (code-location-%tlf-offset code-location)
  2312.             (code-location-%tlf-offset loc))
  2313.           (setf (code-location-%form-number code-location)
  2314.             (code-location-%form-number loc))
  2315.           (setf (compiled-code-location-%live-set code-location)
  2316.             (compiled-code-location-%live-set loc))
  2317.           (setf (compiled-code-location-kind code-location)
  2318.             (compiled-code-location-kind loc))
  2319.           (return-from fill-in-code-location t))))))))
  2320.  
  2321.  
  2322.  
  2323. ;;;; Debug-blocks.
  2324.  
  2325. ;;; DO-DEBUG-BLOCK-LOCATIONS -- Public.
  2326. ;;;
  2327. (defmacro do-debug-block-locations ((code-var debug-block &optional return)
  2328.                     &body body)
  2329.   "Executes forms in a context with code-var bound to each code-location in
  2330.    debug-block.  This returns the value of executing result (defaults to nil)."
  2331.   (let ((code-locations (gensym))
  2332.     (i (gensym)))
  2333.     `(let ((,code-locations (debug-block-code-locations ,debug-block)))
  2334.        (declare (simple-vector ,code-locations))
  2335.        (dotimes (,i (length ,code-locations) ,return)
  2336.      (let ((,code-var (svref ,code-locations ,i)))
  2337.        ,@body)))))
  2338.  
  2339. ;;; DEBUG-BLOCK-FUNCTION-NAME -- Internal.
  2340. ;;;
  2341. (defun debug-block-function-name (debug-block)
  2342.   "Returns the name of the function represented by debug-function.  This may
  2343.    be a string or a cons; do not assume it is a symbol."
  2344.   (etypecase debug-block
  2345.     (compiled-debug-block
  2346.      (let ((code-locs (compiled-debug-block-code-locations debug-block)))
  2347.        (declare (simple-vector code-locs))
  2348.        (if (zerop (length code-locs))
  2349.        "??? Can't get name of debug-block's function."
  2350.        (debug-function-name
  2351.         (code-location-debug-function (svref code-locs 0))))))
  2352.     (interpreted-debug-block
  2353.      (c::lambda-name (c::block-home-lambda
  2354.               (interpreted-debug-block-ir1-block debug-block))))))
  2355.  
  2356.  
  2357. ;;; DEBUG-BLOCK-CODE-LOCATIONS -- Internal.
  2358. ;;;
  2359. (defun debug-block-code-locations (debug-block)
  2360.   (etypecase debug-block
  2361.     (compiled-debug-block
  2362.      (compiled-debug-block-code-locations debug-block))
  2363.     (interpreted-debug-block
  2364.      (interpreted-debug-block-code-locations debug-block))))
  2365.  
  2366. ;;; INTERPRETED-DEBUG-BLOCK-CODE-LOCATIONS -- Internal.
  2367. ;;;
  2368. (defun interpreted-debug-block-code-locations (debug-block)
  2369.   (let ((code-locs (interpreted-debug-block-locations debug-block)))
  2370.     (if (eq code-locs :unparsed)
  2371.     (with-parsing-buffer (buf)
  2372.       (c::do-nodes (node cont (interpreted-debug-block-ir1-block
  2373.                    debug-block))
  2374.         (vector-push-extend (make-interpreted-code-location
  2375.                  node
  2376.                  (make-interpreted-debug-function
  2377.                   (c::block-home-lambda (c::node-block node))))
  2378.                 buf))
  2379.       (setf (interpreted-debug-block-locations debug-block)
  2380.         (result buf)))
  2381.     code-locs)))
  2382.  
  2383.  
  2384.  
  2385. ;;;; Variables.
  2386.  
  2387. ;;; DEBUG-VARIABLE-SYMBOL -- Public.
  2388. ;;;
  2389. (defun debug-variable-symbol (debug-var)
  2390.   "Returns the symbol from interning DEBUG-VARIABLE-NAME in the package named
  2391.    by DEBUG-VARIABLE-PACKAGE."
  2392.   (let ((package (debug-variable-package debug-var)))
  2393.     (if package
  2394.     (intern (debug-variable-name debug-var) package)
  2395.     (make-symbol (debug-variable-name debug-var)))))
  2396.  
  2397. ;;; DEBUG-VARIABLE-VALID-VALUE -- Public.
  2398. ;;;
  2399. (defun debug-variable-valid-value (debug-var frame)
  2400.   "Returns the value stored for debug-variable in frame.  If the value is not
  2401.    :valid, then this signals an invalid-value error."
  2402.   (unless (eq (debug-variable-validity debug-var (frame-code-location frame))
  2403.           :valid)
  2404.     (error 'invalid-value :debug-variable debug-var :frame frame))
  2405.   (debug-variable-value debug-var frame))
  2406.  
  2407. ;;; DEBUG-VARIABLE-VALUE -- Public.
  2408. ;;;
  2409. (defun debug-variable-value (debug-var frame)
  2410.   "Returns the value stored for debug-variable in frame.  The value may be
  2411.    invalid.  This is SETF'able."
  2412.   (etypecase debug-var
  2413.     (compiled-debug-variable
  2414.      (check-type frame compiled-frame)
  2415.      (let ((res (access-compiled-debug-var-slot debug-var frame)))
  2416.        (if (indirect-value-cell-p res)
  2417.        (system:%primitive c::value-cell-ref res)
  2418.        res)))
  2419.     (interpreted-debug-variable
  2420.      (check-type frame interpreted-frame)
  2421.      (eval::leaf-value-lambda-var
  2422.       (interpreted-code-location-ir1-node (frame-code-location frame))
  2423.       (interpreted-debug-variable-ir1-var debug-var)
  2424.       (frame-pointer frame)
  2425.       (interpreted-frame-closure frame)))))
  2426.  
  2427.  
  2428. ;;; ACCESS-COMPILED-DEBUG-VAR-SLOT -- Internal.
  2429. ;;;
  2430. ;;; This returns what is stored for the variable represented by debug-var
  2431. ;;; relative to the frame.  This may be an indirect value cell if the
  2432. ;;; variable is both closed over and set.
  2433. ;;;
  2434. (defun access-compiled-debug-var-slot (debug-var frame)
  2435.   (let ((escaped (compiled-frame-escaped frame)))
  2436.     (if escaped
  2437.     (sub-access-debug-var-slot
  2438.      (frame-pointer frame)
  2439.      (compiled-debug-variable-sc-offset debug-var)
  2440.      escaped)
  2441.     (sub-access-debug-var-slot
  2442.      (frame-pointer frame)
  2443.      (or (compiled-debug-variable-save-sc-offset debug-var)
  2444.          (compiled-debug-variable-sc-offset debug-var))))))
  2445.  
  2446. ;;; SUB-ACCESS-DEBUG-VAR-SLOT -- Internal.
  2447. ;;;
  2448. (defun sub-access-debug-var-slot (fp sc-offset &optional escaped)
  2449.   (macrolet ((with-escaped-value ((var) &body forms)
  2450.            `(if escaped
  2451.             (let ((,var (vm:sigcontext-register
  2452.                  escaped
  2453.                  (c::sc-offset-offset sc-offset))))
  2454.               ,@forms)
  2455.             :invalid-value-for-unescaped-register-storage))
  2456.          (escaped-float-value (format)
  2457.            `(if escaped
  2458.             (vm:sigcontext-float-register
  2459.              escaped
  2460.              (c::sc-offset-offset sc-offset)
  2461.              ',format)
  2462.             :invalid-value-for-unescaped-register-storage))
  2463.          (with-nfp ((var) &body body)
  2464.            `(let ((,var (if escaped
  2465.                 (system:int-sap
  2466.                  (vm:sigcontext-register escaped
  2467.                              vm::nfp-offset))
  2468.                 (system:sap-ref-sap fp (* vm::nfp-save-offset
  2469.                               vm:word-bytes)))))
  2470.           ,@body)))
  2471.     (ecase (c::sc-offset-scn sc-offset)
  2472.       ((#.vm:any-reg-sc-number
  2473.     #.vm:descriptor-reg-sc-number
  2474.     #+rt #.vm:word-pointer-reg-sc-number)
  2475.        (system:without-gcing
  2476.     (with-escaped-value (val)
  2477.       (kernel:make-lisp-obj val))))
  2478.       (#.vm:base-char-reg-sc-number
  2479.        (with-escaped-value (val)
  2480.      (code-char val)))
  2481.       (#.vm:sap-reg-sc-number
  2482.        (with-escaped-value (val)
  2483.      (system:int-sap val)))
  2484.       (#.vm:signed-reg-sc-number
  2485.        (with-escaped-value (val)
  2486.      (if (logbitp (1- vm:word-bits) val)
  2487.          (logior val (ash -1 vm:word-bits))
  2488.          val)))
  2489.       (#.vm:unsigned-reg-sc-number
  2490.        (with-escaped-value (val)
  2491.      val))
  2492.       (#.vm:non-descriptor-reg-sc-number
  2493.        (error "Local non-descriptor register access?"))
  2494.       (#.vm:interior-reg-sc-number
  2495.        (error "Local interior register access?"))
  2496.       (#.vm:single-reg-sc-number
  2497.        (escaped-float-value single-float))
  2498.       (#.vm:double-reg-sc-number
  2499.        (escaped-float-value double-float))
  2500.       (#.vm:single-stack-sc-number
  2501.        (with-nfp (nfp)
  2502.      (system:sap-ref-single nfp (* (vm::sc-offset-offset sc-offset)
  2503.                        vm:word-bytes))))
  2504.       (#.vm:double-stack-sc-number
  2505.        (with-nfp (nfp)
  2506.      (system:sap-ref-double nfp (* (c::sc-offset-offset sc-offset)
  2507.                        vm:word-bytes))))
  2508.       (#.vm:control-stack-sc-number
  2509.        (kernel:stack-ref fp (c::sc-offset-offset sc-offset)))
  2510.       (#.vm:base-char-stack-sc-number
  2511.        (with-nfp (nfp)
  2512.      (code-char (system:sap-ref-32 nfp (* (c::sc-offset-offset sc-offset)
  2513.                           vm:word-bytes)))))
  2514.       (#.vm:unsigned-stack-sc-number
  2515.        (with-nfp (nfp)
  2516.      (system:sap-ref-32 nfp (* (c::sc-offset-offset sc-offset)
  2517.                    vm:word-bytes))))
  2518.       (#.vm:signed-stack-sc-number
  2519.        (with-nfp (nfp)
  2520.      (system:signed-sap-ref-32 nfp (* (c::sc-offset-offset sc-offset)
  2521.                       vm:word-bytes))))
  2522.       (#.vm:sap-stack-sc-number
  2523.        (with-nfp (nfp)
  2524.      (system:sap-ref-sap nfp (* (c::sc-offset-offset sc-offset)
  2525.                     vm:word-bytes)))))))
  2526.  
  2527.  
  2528. ;;; %SET-DEBUG-VARIABLE-VALUE -- Internal.
  2529. ;;;
  2530. ;;; This stores value as the value of debug-var in frame.  In the
  2531. ;;; compiled-debug-variable case, access the current value to determine if it
  2532. ;;; is an indirect value cell.  This occurs when the variable is both closed
  2533. ;;; over and set.  For interpreted-debug-variables just call
  2534. ;;; EVAL::SET-LEAF-VALUE-LAMBDA-VAR with the right interpreter objects.
  2535. ;;;
  2536. (defun %set-debug-variable-value (debug-var frame value)
  2537.   (etypecase debug-var
  2538.     (compiled-debug-variable
  2539.      (check-type frame compiled-frame)
  2540.      (let ((current-value (access-compiled-debug-var-slot debug-var frame)))
  2541.        (if (indirect-value-cell-p current-value)
  2542.        (system:%primitive c::value-cell-set current-value value)
  2543.        (set-compiled-debug-variable-slot debug-var frame value))))
  2544.     (interpreted-debug-variable
  2545.      (check-type frame interpreted-frame)
  2546.      (eval::set-leaf-value-lambda-var
  2547.       (interpreted-code-location-ir1-node (frame-code-location frame))
  2548.       (interpreted-debug-variable-ir1-var debug-var)
  2549.       (frame-pointer frame)
  2550.       (interpreted-frame-closure frame)
  2551.       value)))
  2552.   value)
  2553. ;;;
  2554. (defsetf debug-variable-value %set-debug-variable-value)
  2555.  
  2556. ;;; SET-COMPILED-DEBUG-VARIABLE-SLOT -- Internal.
  2557. ;;;
  2558. ;;; This stores value for the variable represented by debug-var relative to the
  2559. ;;; frame.  This assumes the location directly contains the variable's value;
  2560. ;;; that is, there is no indirect value cell currently there in case the
  2561. ;;; variable is both closed over and set.
  2562. ;;;
  2563. (defun set-compiled-debug-variable-slot (debug-var frame value)
  2564.   (let ((escaped (compiled-frame-escaped frame)))
  2565.     (if escaped
  2566.     (sub-set-debug-var-slot (frame-pointer frame)
  2567.                 (compiled-debug-variable-sc-offset debug-var)
  2568.                 value escaped)
  2569.     (sub-set-debug-var-slot
  2570.      (frame-pointer frame)
  2571.      (or (compiled-debug-variable-save-sc-offset debug-var)
  2572.          (compiled-debug-variable-sc-offset debug-var))
  2573.      value))))
  2574.  
  2575. ;;; SUB-SET-DEBUG-VAR-SLOT -- Internal.
  2576. ;;;
  2577. (defun sub-set-debug-var-slot (fp sc-offset value &optional escaped)
  2578.   (macrolet ((set-escaped-value (val)
  2579.            `(if escaped
  2580.             (setf (vm:sigcontext-register
  2581.                escaped
  2582.                (c::sc-offset-offset sc-offset))
  2583.               ,val)
  2584.             value))
  2585.          (set-escaped-float-value (format val)
  2586.            `(if escaped
  2587.             (setf (vm:sigcontext-float-register
  2588.                escaped
  2589.                (c::sc-offset-offset sc-offset)
  2590.                ',format)
  2591.               ,val)
  2592.             value))
  2593.          (with-nfp ((var) &body body)
  2594.            `(let ((,var (if escaped
  2595.                 (system:int-sap
  2596.                  (vm:sigcontext-register escaped
  2597.                              vm::nfp-offset))
  2598.                 (system:sap-ref-sap fp
  2599.                             (* vm::nfp-save-offset
  2600.                                vm:word-bytes)))))
  2601.           ,@body)))
  2602.     (ecase (c::sc-offset-scn sc-offset)
  2603.       ((#.vm:any-reg-sc-number
  2604.     #.vm:descriptor-reg-sc-number
  2605.     #+rt #.vm:word-pointer-reg-sc-number)
  2606.        (system:without-gcing
  2607.     (set-escaped-value
  2608.       (kernel:get-lisp-obj-address value))))
  2609.       (#.vm:base-char-reg-sc-number
  2610.        (set-escaped-value (char-code value)))
  2611.       (#.vm:sap-reg-sc-number
  2612.        (set-escaped-value (system:sap-int value)))
  2613.       (#.vm:signed-reg-sc-number
  2614.        (set-escaped-value (logand value (1- (ash 1 vm:word-bits)))))
  2615.       (#.vm:unsigned-reg-sc-number
  2616.        (set-escaped-value value))
  2617.       (#.vm:non-descriptor-reg-sc-number
  2618.        (error "Local non-descriptor register access?"))
  2619.       (#.vm:interior-reg-sc-number
  2620.        (error "Local interior register access?"))
  2621.       (#.vm:single-reg-sc-number
  2622.        (set-escaped-float-value single-float value))
  2623.       (#.vm:double-reg-sc-number
  2624.        (set-escaped-float-value double-float value))
  2625.       (#.vm:single-stack-sc-number
  2626.        (with-nfp (nfp)
  2627.      (setf (system:sap-ref-single nfp (* (c::sc-offset-offset sc-offset)
  2628.                          vm:word-bytes))
  2629.            (the single-float value))))
  2630.       (#.vm:double-stack-sc-number
  2631.        (with-nfp (nfp)
  2632.      (setf (system:sap-ref-double nfp (* (c::sc-offset-offset sc-offset)
  2633.                          vm:word-bytes))
  2634.            (the double-float value))))
  2635.       (#.vm:control-stack-sc-number
  2636.        (setf (kernel:stack-ref fp (c::sc-offset-offset sc-offset)) value))
  2637.       (#.vm:base-char-stack-sc-number
  2638.        (with-nfp (nfp)
  2639.      (setf (system:sap-ref-32 nfp (* (c::sc-offset-offset sc-offset)
  2640.                      vm:word-bytes))
  2641.            (char-code (the character value)))))
  2642.       (#.vm:unsigned-stack-sc-number
  2643.        (with-nfp (nfp)
  2644.      (setf (system:sap-ref-32 nfp (* (c::sc-offset-offset sc-offset)
  2645.                      vm:word-bytes))
  2646.            (the (unsigned-byte 32) value))))
  2647.       (#.vm:signed-stack-sc-number
  2648.        (with-nfp (nfp)
  2649.      (setf (system:signed-sap-ref-32 nfp (* (c::sc-offset-offset sc-offset)
  2650.                         vm:word-bytes))
  2651.            (the (signed-byte 32) value))))
  2652.       (#.vm:sap-stack-sc-number
  2653.        (with-nfp (nfp)
  2654.      (setf (system:sap-ref-sap nfp (* (c::sc-offset-offset sc-offset)
  2655.                       vm:word-bytes))
  2656.            (the system:system-area-pointer value)))))))
  2657.  
  2658. (defsetf debug-variable-value %set-debug-variable-value)
  2659.  
  2660.  
  2661. ;;; INDIRECT-VALUE-CELL-P -- Internal.
  2662. ;;;
  2663. ;;; The method for setting and accessing compiled-debug-variable values use
  2664. ;;; this to determine if the value stored is the actual value or an indirection
  2665. ;;; cell.
  2666. ;;;
  2667. (defun indirect-value-cell-p (x)
  2668.   (and (= (kernel:get-lowtag x) vm:other-pointer-type)
  2669.        (= (kernel:get-type x) vm:value-cell-header-type)))
  2670.  
  2671.  
  2672. ;;; DEBUG-VARIABLE-VALIDITY -- Public.
  2673. ;;;
  2674. ;;; If the variable is always alive, then it is valid.  If the code-location is
  2675. ;;; unknown, then the variable's validity is :unknown.  Once we've called
  2676. ;;; CODE-LOCATION-UNKNOWN-P, we know the live-set information has been cached
  2677. ;;; in the code-location.
  2678. ;;;
  2679. (defun debug-variable-validity (debug-var basic-code-loc)
  2680.   "Returns three values reflecting the validity of debug-variable's value
  2681.    at basic-code-location:
  2682.       :valid    The value is known to be available.
  2683.       :invalid  The value is known to be unavailable.
  2684.       :unknown  The value's availability is unknown."
  2685.   (etypecase debug-var
  2686.     (compiled-debug-variable
  2687.      (compiled-debug-variable-validity debug-var basic-code-loc))
  2688.     (interpreted-debug-variable
  2689.      (check-type basic-code-loc interpreted-code-location)
  2690.      (let ((validp (rassoc (interpreted-debug-variable-ir1-var debug-var)
  2691.                (c::lexenv-variables
  2692.                 (c::node-lexenv
  2693.                  (interpreted-code-location-ir1-node
  2694.                   basic-code-loc))))))
  2695.        (if validp :valid :invalid)))))
  2696.  
  2697. ;;; COMPILED-DEBUG-VARIABLE-VALIDITY -- Internal.
  2698. ;;;
  2699. ;;; This is the method for DEBUG-VARIABLE-VALIDITY for compiled-debug-variables.
  2700. ;;; For safety, make sure basic-code-loc is what we think.
  2701. ;;;
  2702. (defun compiled-debug-variable-validity (debug-var basic-code-loc)
  2703.   (check-type basic-code-loc compiled-code-location)
  2704.   (cond ((debug-variable-alive-p debug-var)
  2705.      (let ((debug-fun (code-location-debug-function basic-code-loc)))
  2706.        (if (>= (compiled-code-location-pc basic-code-loc)
  2707.            (c::compiled-debug-function-start-pc
  2708.             (compiled-debug-function-compiler-debug-fun debug-fun)))
  2709.            :valid
  2710.            :invalid)))
  2711.     ((code-location-unknown-p basic-code-loc) :unknown)
  2712.     (t
  2713.      (let ((pos (position debug-var
  2714.                   (debug-function-debug-variables
  2715.                    (code-location-debug-function basic-code-loc)))))
  2716.        (unless pos
  2717.          (error 'unknown-debug-variable
  2718.             :debug-variable debug-var
  2719.             :debug-function
  2720.             (code-location-debug-function basic-code-loc)))
  2721.        ;; There must be live-set info since basic-code-loc is known.
  2722.        (if (zerop (sbit (compiled-code-location-live-set basic-code-loc)
  2723.                 pos))
  2724.            :invalid
  2725.            :valid)))))
  2726.  
  2727.  
  2728.  
  2729. ;;;; Sources.
  2730.  
  2731. ;;; Written by Rob Maclachlan.
  2732. ;;; Documented by Bill Chiles.
  2733. ;;;
  2734. ;;; This code produces and uses what we call source-paths.  A source-path is a
  2735. ;;; list whose first element is a form number as returned by
  2736. ;;; CODE-LOCATION-FORM-NUMBER and whose last element is a top-level-form number
  2737. ;;; as returned by CODE-LOCATION-TOP-LEVEL-FORM-NUMBER.  The elements from the
  2738. ;;; last to the first, exclusively, are the numbered subforms into which to
  2739. ;;; descend.  For example:
  2740. ;;;    (defun foo (x)
  2741. ;;;      (let ((a (aref x 3)))
  2742. ;;;        (cons a 3)))
  2743. ;;; The call to AREF in this example is form number 5.  Assuming this DEFUN is
  2744. ;;; the 11'th top-level-form, the source-path for the AREF call is as follows:
  2745. ;;;    (5 1 0 1 3 11)
  2746. ;;; Given the DEFUN, 3 gets you the LET, 1 gets you the bindings, 0 gets the
  2747. ;;; first binding, and 1 gets the AREF form.
  2748. ;;;
  2749.  
  2750.  
  2751. ;;; Temporary buffer used to build form-number => source-path translation in
  2752. ;;; FORM-NUMBER-TRANSLATIONS.
  2753. ;;;
  2754. (defvar *form-number-temp* (make-array 10 :fill-pointer 0 :adjustable t))
  2755.  
  2756. ;;; Table used to detect CAR circularities in FORM-NUMBER-TRANSLATIONS.
  2757. ;;;
  2758. (defvar *form-number-circularity-table* (make-hash-table :test #'eq))
  2759.  
  2760. ;;; FORM-NUMBER-TRANSLATIONS  --  Public.
  2761. ;;;
  2762. ;;; The vector elements are in the same format as the compiler's
  2763. ;;; NODE-SOUCE-PATH; that is, the first element is the form number and the last
  2764. ;;; is the top-level-form number.
  2765. ;;;
  2766. (defun form-number-translations (form tlf-number)
  2767.   "This returns a table mapping form numbers to source-paths.  A source-path
  2768.    indicates a descent into the top-level-form form, going directly to the
  2769.    subform corressponding to the form number."
  2770.   (clrhash *form-number-circularity-table*)
  2771.   (setf (fill-pointer *form-number-temp*) 0)
  2772.   (sub-translate-form-numbers form (list tlf-number))
  2773.   (coerce *form-number-temp* 'simple-vector))
  2774. ;;;
  2775. (defun sub-translate-form-numbers (form path)
  2776.   (unless (gethash form *form-number-circularity-table*)
  2777.     (setf (gethash form *form-number-circularity-table*) t)
  2778.     (vector-push-extend (cons (fill-pointer *form-number-temp*) path)
  2779.             *form-number-temp*)
  2780.     (let ((pos 0)
  2781.       (subform form)
  2782.       (trail form))
  2783.       (declare (fixnum pos))
  2784.       (macrolet ((frob ()
  2785.            '(progn
  2786.               (when (atom subform) (return))
  2787.               (let ((fm (car subform)))
  2788.             (when (consp fm)
  2789.               (sub-translate-form-numbers fm (cons pos path)))
  2790.             (incf pos))
  2791.               (setq subform (cdr subform))
  2792.               (when (eq subform trail) (return)))))
  2793.     (loop
  2794.       (frob)
  2795.       (frob)
  2796.       (setq trail (cdr trail)))))))
  2797.  
  2798.  
  2799. ;;; SOURCE-PATH-CONTEXT  --  Public.
  2800. ;;;
  2801. (defun source-path-context (form path context)
  2802.   "Form is a top-level form, and path is a source-path into it.  This returns
  2803.    the form indicated by the source-path.  Context is the number of enclosing
  2804.    forms to return instead of directly returning the source-path form.  When
  2805.    context is non-zero, the form returned contains a marker, #:****HERE****,
  2806.    immediately before the form indicated by path."
  2807.   (declare (type unsigned-byte context))
  2808.   ;;
  2809.   ;; Get to the form indicated by path or the enclosing form indicated by
  2810.   ;; context and path.
  2811.   (let ((path (reverse (butlast (cdr path)))))
  2812.     (dotimes (i (- (length path) context))
  2813.       (setq form (elt form (first path)))
  2814.       (setq path (rest path)))
  2815.     ;;
  2816.     ;; Recursively rebuild the source form resulting from the above descent,
  2817.     ;; copying the beginning of each subform up to the next subform we descend
  2818.     ;; into according to path.  At the bottom of the recursion, we return the
  2819.     ;; form indicated by path preceded by our marker, and this gets spliced
  2820.     ;; into the resulting list structure on the way back up.
  2821.     (labels ((frob (form path level)
  2822.            (if (or (zerop level) (null path))
  2823.            (if (zerop context)
  2824.                form
  2825.                `(#:***here*** ,form))
  2826.            (let* ((n (first path))
  2827.               (res (frob (elt form n) (rest path) (1- level))))
  2828.              (nconc (subseq form 0 n)
  2829.                 (cons res (nthcdr (1+ n) form)))))))
  2830.       (frob form path context))))
  2831.  
  2832.  
  2833. ;;;; PREPROCESS-FOR-EVAL and EVAL-IN-FRAME.
  2834.  
  2835. ;;; PREPROCESS-FOR-EVAL  --  Public.
  2836. ;;;
  2837. ;;; Create a SYMBOL-MACROLET for each variable valid at the location which
  2838. ;;; accesses that variable from the frame argument.
  2839. ;;;
  2840. (defun preprocess-for-eval (form loc)
  2841.   "Return a function of one argument that evaluates form in the lexical
  2842.    context of the basic-code-location loc.  PREPROCESS-FOR-EVAL signals a
  2843.    no-debug-variables condition when the loc's debug-function has no
  2844.    debug-variable information available.  The returned function takes the frame
  2845.    to get values from as its argument, and it returns the values of form.
  2846.    The returned function signals the following conditions: invalid-value,
  2847.    ambiguous-variable-name, and frame-function-mismatch"
  2848.   (declare (type code-location loc))
  2849.   (let ((n-frame (gensym))
  2850.     (fun (code-location-debug-function loc)))
  2851.     (unless (debug-variable-info-available fun)
  2852.       (debug-signal 'no-debug-variables :debug-function fun))
  2853.     (ext:collect ((binds)
  2854.           (specs))
  2855.       (do-debug-function-variables (var fun)
  2856.     (let ((validity (debug-variable-validity var loc)))
  2857.       (unless (eq validity :invalid)
  2858.         (let* ((sym (debug-variable-symbol var))
  2859.            (found (assoc sym (binds))))
  2860.           (if found
  2861.           (setf (second found) :ambiguous)
  2862.           (binds (list sym validity var)))))))
  2863.       (dolist (bind (binds))
  2864.     (let ((name (first bind))
  2865.           (var (third bind)))
  2866.       (ecase (second bind)
  2867.         (:valid
  2868.          (specs `(,name (debug-variable-value ',var ,n-frame))))
  2869.         (:unknown
  2870.          (specs `(,name (debug-signal 'invalid-value :debug-variable ',var
  2871.                       :frame ,n-frame))))
  2872.         (:ambiguous
  2873.          (specs `(,name (debug-signal 'ambiguous-variable-name :name ',name
  2874.                       :frame ,n-frame)))))))
  2875.       (let ((res (coerce `(lambda (,n-frame)
  2876.                 (declare (ignorable ,n-frame))
  2877.                 (symbol-macrolet ,(specs) ,form))
  2878.              'function)))
  2879.     #'(lambda (frame)
  2880.         ;; This prevents these functions from use in any location other
  2881.         ;; than a function return location, so maybe this should only
  2882.         ;; check whether frame's debug-function is the same as loc's.
  2883.         (unless (code-location= (frame-code-location frame) loc)
  2884.           (debug-signal 'frame-function-mismatch
  2885.                 :code-location loc :form form :frame frame))
  2886.         (funcall res frame))))))
  2887.  
  2888.  
  2889. ;;; EVAL-IN-FRAME  --  Public.
  2890. ;;;
  2891. (defun eval-in-frame (frame form)
  2892.   (declare (type frame frame))
  2893.   "Evaluate Form in the lexical context of Frame's current code location,
  2894.    returning the results of the evaluation."
  2895.   (funcall (preprocess-for-eval form (frame-code-location frame)) frame))
  2896.  
  2897.  
  2898.  
  2899. ;;;; Breakpoints.
  2900.  
  2901. ;;;
  2902. ;;; User visible interface.
  2903. ;;;
  2904.  
  2905. (defun make-breakpoint (hook-function what
  2906.             &key (kind :code-location) info function-end-cookie)
  2907.   "This creates and returns a breakpoint.  When program execution encounters
  2908.    the breakpoint, the system calls hook-function.  Hook-function takes the
  2909.    current frame for the function in which the program is running and the
  2910.    breakpoint object.
  2911.       What and kind determine where in a function the system invokes
  2912.    hook-function.  What is either a code-location or a debug-function.  Kind is
  2913.    one of :code-location, :function-start, or :function-end.  Since the starts
  2914.    and ends of functions may not have code-locations representing them,
  2915.    designate these places by supplying what as a debug-function and kind
  2916.    indicating the :function-start or :function-end.  When what is a
  2917.    debug-function and kind is :function-end, then hook-function must take two
  2918.    additional arguments, a list of values returned by the function and a
  2919.    function-end-cookie.
  2920.       Info is information supplied by and used by the user.
  2921.       Function-end-cookie is a function.  To implement :function-end breakpoints,
  2922.    the system uses starter breakpoints to establish the :function-end breakpoint
  2923.    for each invocation of the function.  Upon each entry, the system creates a
  2924.    unique cookie to identify the invocation, and when the user supplies a
  2925.    function for this argument, the system invokes it on the frame and the
  2926.    cookie.  The system later invokes the :function-end breakpoint hook on the
  2927.    same cookie.  The user may save the cookie for comparison in the hook
  2928.    function.
  2929.       This signals an error if what is an unknown code-location."
  2930.   (etypecase what
  2931.     (code-location
  2932.      (when (code-location-unknown-p what)
  2933.        (error "Cannot make a breakpoint at an unknown code location -- ~S."
  2934.           what))
  2935.      (assert (eq kind :code-location))
  2936.      (let ((bpt (%make-breakpoint hook-function what kind info)))
  2937.        (etypecase what
  2938.      (interpreted-code-location
  2939.       (error "Breakpoints in interpreted code are currently unsupported."))
  2940.      (compiled-code-location
  2941.       ;; This slot is filled in due to calling CODE-LOCATION-UNKNOWN-P.
  2942.       (when (eq (compiled-code-location-kind what) :unknown-return)
  2943.         (let ((other-bpt (%make-breakpoint hook-function what
  2944.                            :unknown-return-partner
  2945.                            info)))
  2946.           (setf (breakpoint-unknown-return-partner bpt) other-bpt)
  2947.           (setf (breakpoint-unknown-return-partner other-bpt) bpt)))))
  2948.        bpt))
  2949.     (compiled-debug-function
  2950.      (ecase kind
  2951.        (:function-start
  2952.     (%make-breakpoint hook-function what kind info))
  2953.        (:function-end
  2954.     (unless (eq (c::compiled-debug-function-returns
  2955.              (compiled-debug-function-compiler-debug-fun what))
  2956.             :standard)
  2957.       (error ":FUNCTION-END breakpoints are currently unsupported ~
  2958.           for the known return convention."))
  2959.           
  2960.     (let* ((bpt (%make-breakpoint hook-function what kind info))
  2961.            (starter (compiled-debug-function-end-starter what)))
  2962.       (unless starter
  2963.         (setf starter (%make-breakpoint #'list what :function-start nil))
  2964.         (setf (breakpoint-hook-function starter)
  2965.           (function-end-starter-hook starter what))
  2966.         (setf (compiled-debug-function-end-starter what) starter))
  2967.       (setf (breakpoint-start-helper bpt) starter)
  2968.       (push bpt (breakpoint-%info starter))
  2969.       (setf (breakpoint-cookie-fun bpt) function-end-cookie)
  2970.       bpt))))
  2971.     (interpreted-debug-function
  2972.      (error ":function-end breakpoints are currently unsupported ~
  2973.          for interpreted-debug-functions."))))
  2974.  
  2975. ;;; These are unique objects created upon entry into a function by a
  2976. ;;; :function-end breakpoint's starter hook.  These are only created when users
  2977. ;;; supply :function-end-cookie to MAKE-BREAKPOINT.  Also, the :function-end
  2978. ;;; breakpoint's hook is called on the same cookie when it is created.
  2979. ;;;
  2980. (defstruct (function-end-cookie
  2981.         (:print-function (lambda (obj str n)
  2982.                    (declare (ignore obj n))
  2983.                    (write-string "#<Function-End-Cookie>" str)))
  2984.         (:constructor make-function-end-cookie (bogus-lra debug-fun)))
  2985.   ;; This is a pointer to the bogus-lra created for :function-end bpts.
  2986.   bogus-lra
  2987.   ;; This is the debug-function associated with the cookie.
  2988.   debug-fun)
  2989.  
  2990. ;;; This maps bogus-lra-components to cookies, so
  2991. ;;; HANDLE-FUNCTION-END-BREAKPOINT can find the appropriate cookie for the
  2992. ;;; breakpoint hook.
  2993. ;;;
  2994. (defvar *function-end-cookies* (make-hash-table :test #'eq))
  2995.  
  2996. ;;; FUNCTION-END-STARTER-HOOK -- Internal.
  2997. ;;;
  2998. ;;; This returns a hook function for the start helper breakpoint associated
  2999. ;;; with a :function-end breakpoint.  The returned function makes a fake LRA
  3000. ;;; that all returns go through, and this piece of fake code actually breaks.
  3001. ;;; Upon return from the break, the code provides the returnee with any values.
  3002. ;;; Since the returned function effectively activates fun-end-bpt on each entry
  3003. ;;; to debug-fun's function, we must establish breakpoint-data about
  3004. ;;; fun-end-bpt.
  3005. ;;;
  3006. (defun function-end-starter-hook (starter-bpt debug-fun)
  3007.   (declare (type breakpoint starter-bpt)
  3008.        (type compiled-debug-function debug-fun))
  3009.   #'(lambda (frame breakpoint)
  3010.       (declare (ignore breakpoint)
  3011.            (type frame frame))
  3012.       (let ((lra-sc-offset
  3013.          (c::compiled-debug-function-return-pc
  3014.           (compiled-debug-function-compiler-debug-fun debug-fun))))
  3015.     (multiple-value-bind (lra component offset)
  3016.                  (make-bogus-lra
  3017.                   (get-context-value frame vm::lra-save-offset
  3018.                          lra-sc-offset))
  3019.       (setf (get-context-value frame vm::lra-save-offset lra-sc-offset)
  3020.         lra)
  3021.       (let ((end-bpts (breakpoint-%info starter-bpt)))
  3022.         (let ((data (breakpoint-data component offset)))
  3023.           (setf (breakpoint-data-breakpoints data) end-bpts)
  3024.           (dolist (bpt end-bpts)
  3025.         (setf (breakpoint-internal-data bpt) data)))
  3026.         (let ((cookie (make-function-end-cookie lra debug-fun)))
  3027.           (setf (gethash component *function-end-cookies*) cookie)
  3028.           (dolist (bpt end-bpts)
  3029.         (let ((fun (breakpoint-cookie-fun bpt)))
  3030.           (when fun (funcall fun frame cookie))))))))))
  3031.  
  3032. ;;; FUNCTION-END-COOKIE-VALID-P -- Public.
  3033. ;;;
  3034. (defun function-end-cookie-valid-p (frame cookie)
  3035.   "This takes a function-end-cookie and a frame, and it returns whether the
  3036.    cookie is still valid.  A cookie becomes invalid when the frame that
  3037.    established the cookie has exited.  Sometimes cookie holders are unaware
  3038.    of cookie invalidation because their :function-end breakpoint hooks didn't
  3039.    run due to THROW'ing.  This takes a frame as an efficiency hack since the
  3040.    user probably has a frame object in hand when using this routine, and it
  3041.    saves repeated parsing of the stack and consing when asking whether a
  3042.    series of cookies is valid."
  3043.   (let ((lra (function-end-cookie-bogus-lra cookie))
  3044.     (lra-sc-offset (c::compiled-debug-function-return-pc
  3045.             (compiled-debug-function-compiler-debug-fun
  3046.              (function-end-cookie-debug-fun cookie)))))
  3047.     (do ((frame frame (frame-down frame)))
  3048.     ((not frame) nil)
  3049.       (when (and (compiled-frame-p frame)
  3050.          (eq lra
  3051.              (get-context-value frame
  3052.                     vm::lra-save-offset
  3053.                     lra-sc-offset)))
  3054.     (return t)))))
  3055.  
  3056. ;;;
  3057. ;;; ACTIVATE-BREAKPOINT.
  3058. ;;;
  3059.  
  3060. ;;; ACTIVATE-BREAKPOINT -- Public.
  3061. ;;;
  3062. (defun activate-breakpoint (breakpoint)
  3063.   "This causes the system to invoke the breakpoint's hook-function until the
  3064.    next call to DEACTIVATE-BREAKPOINT or DELETE-BREAKPOINT.  The system invokes
  3065.    breakpoint hook functions in the opposite order that you activate them."
  3066.   (when (eq (breakpoint-status breakpoint) :deleted)
  3067.     (error "Cannot activate a deleted breakpoint -- ~S." breakpoint))
  3068.   (unless (eq (breakpoint-status breakpoint) :active)
  3069.     (ecase (breakpoint-kind breakpoint)
  3070.       (:code-location
  3071.        (let ((loc (breakpoint-what breakpoint)))
  3072.      (etypecase loc
  3073.        (interpreted-code-location
  3074.         (error "Breakpoints in interpreted code are currently unsupported."))
  3075.        (compiled-code-location
  3076.         (activate-compiled-code-location-breakpoint breakpoint)
  3077.         (let ((other (breakpoint-unknown-return-partner breakpoint)))
  3078.           (when other
  3079.         (activate-compiled-code-location-breakpoint other)))))))
  3080.       (:function-start
  3081.        (etypecase (breakpoint-what breakpoint)
  3082.      (compiled-debug-function
  3083.       (activate-compiled-function-start-breakpoint breakpoint))
  3084.      (interpreted-debug-function
  3085.       (error "I don't know how you made this, but they're unsupported -- ~S"
  3086.          (breakpoint-what breakpoint)))))
  3087.       (:function-end
  3088.        (etypecase (breakpoint-what breakpoint)
  3089.      (compiled-debug-function
  3090.       (let ((starter (breakpoint-start-helper breakpoint)))
  3091.         (unless (eq (breakpoint-status starter) :active)
  3092.           ;; May already be active by some other :function-end breakpoint.
  3093.           (activate-compiled-function-start-breakpoint starter)))
  3094.       (setf (breakpoint-status breakpoint) :active))
  3095.      (interpreted-debug-function
  3096.       (error "I don't know how you made this, but they're unsupported -- ~S"
  3097.          (breakpoint-what breakpoint)))))))
  3098.   breakpoint)
  3099.  
  3100. ;;; ACTIVATE-COMPILED-CODE-LOCATION-BREAKPOINT -- Internal.
  3101. ;;;
  3102. (defun activate-compiled-code-location-breakpoint (breakpoint)
  3103.   (declare (type breakpoint breakpoint))
  3104.   (let ((loc (breakpoint-what breakpoint)))
  3105.     (declare (type compiled-code-location loc))
  3106.     (sub-activate-breakpoint
  3107.      breakpoint
  3108.      (breakpoint-data (compiled-debug-function-component
  3109.                (code-location-debug-function loc))
  3110.               (+ (compiled-code-location-pc loc)
  3111.              (if (or (eq (breakpoint-kind breakpoint)
  3112.                      :unknown-return-partner)
  3113.                  (eq (compiled-code-location-kind loc)
  3114.                      :single-value-return))
  3115.                  (error "Return location breakpoints NYI.")
  3116.                  0))))))
  3117.  
  3118. ;;; ACTIVATE-COMPILED-FUNCTION-START-BREAKPOINT -- Internal.
  3119. ;;;
  3120. (defun activate-compiled-function-start-breakpoint (breakpoint)
  3121.   (declare (type breakpoint breakpoint))
  3122.   (let ((debug-fun (breakpoint-what breakpoint)))
  3123.     (sub-activate-breakpoint
  3124.      breakpoint
  3125.      (breakpoint-data (compiled-debug-function-component debug-fun)
  3126.               (c::compiled-debug-function-start-pc
  3127.                (compiled-debug-function-compiler-debug-fun
  3128.             debug-fun))))))
  3129.  
  3130. ;;; SUB-ACTIVATE-BREAKPOINT -- Internal.
  3131. ;;;
  3132. (defun sub-activate-breakpoint (breakpoint data)
  3133.   (declare (type breakpoint breakpoint)
  3134.        (type breakpoint-data data))
  3135.   (setf (breakpoint-status breakpoint) :active)
  3136.   (system:without-interrupts
  3137.    (unless (breakpoint-data-breakpoints data)
  3138.      (setf (breakpoint-data-instruction data)
  3139.        (system:without-gcing
  3140.         (breakpoint-install (kernel:get-lisp-obj-address
  3141.                  (breakpoint-data-component data))
  3142.                 (breakpoint-data-offset data)))))
  3143.    (setf (breakpoint-data-breakpoints data)
  3144.      (append (breakpoint-data-breakpoints data) (list breakpoint)))
  3145.    (setf (breakpoint-internal-data breakpoint) data)))
  3146.  
  3147. ;;;
  3148. ;;; DEACTIVATE-BREAKPOINT.
  3149. ;;;
  3150.  
  3151. ;;; DEACTIVATE-BREAKPOINT -- Public.
  3152. ;;;
  3153. (defun deactivate-breakpoint (breakpoint)
  3154.   "This stops the system from invoking the breakpoint's hook-function."
  3155.   (when (eq (breakpoint-status breakpoint) :active)
  3156.     (system:without-interrupts
  3157.      (let ((loc (breakpoint-what breakpoint)))
  3158.        (etypecase loc
  3159.      ((or interpreted-code-location interpreted-debug-function)
  3160.       (error
  3161.        "Breakpoints in interpreted code are currently unsupported."))
  3162.      ((or compiled-code-location compiled-debug-function)
  3163.       (deactivate-compiled-breakpoint breakpoint)
  3164.       (let ((other (breakpoint-unknown-return-partner breakpoint)))
  3165.         (when other
  3166.           (deactivate-compiled-breakpoint other))))))))
  3167.   breakpoint)
  3168.  
  3169. (defun deactivate-compiled-breakpoint (breakpoint)
  3170.   (if (eq (breakpoint-kind breakpoint) :function-end)
  3171.       (let ((starter (breakpoint-start-helper breakpoint)))
  3172.     (unless (find-if #'(lambda (bpt)
  3173.                  (and (not (eq bpt breakpoint))
  3174.                   (eq (breakpoint-status bpt) :active)))
  3175.              (breakpoint-%info starter))
  3176.       (deactivate-compiled-breakpoint starter)))
  3177.       (let* ((data (breakpoint-internal-data breakpoint))
  3178.          (bpts (delete breakpoint (breakpoint-data-breakpoints data))))
  3179.     (setf (breakpoint-internal-data breakpoint) nil)
  3180.     (setf (breakpoint-data-breakpoints data) bpts)
  3181.     (unless bpts
  3182.       (system:without-gcing
  3183.        (breakpoint-remove (kernel:get-lisp-obj-address
  3184.                    (breakpoint-data-component data))
  3185.                   (breakpoint-data-offset data)
  3186.                   (breakpoint-data-instruction data)))
  3187.       (delete-breakpoint-data data))))
  3188.   (setf (breakpoint-status breakpoint) :inactive)
  3189.   breakpoint)
  3190.  
  3191. ;;;
  3192. ;;; BREAKPOINT-INFO.
  3193. ;;;
  3194.  
  3195. ;;; BREAKPOINT-INFO -- Public.
  3196. ;;;
  3197. (defun breakpoint-info (breakpoint)
  3198.   "This returns the user maintained info associated with breakpoint.  This
  3199.    is SETF'able."
  3200.   (breakpoint-%info breakpoint))
  3201. ;;;
  3202. (defun %set-breakpoint-info (breakpoint value)
  3203.   (setf (breakpoint-%info breakpoint) value)
  3204.   (let ((other (breakpoint-unknown-return-partner breakpoint)))
  3205.     (when other
  3206.       (setf (breakpoint-%info other) value))))
  3207. ;;;
  3208. (defsetf breakpoint-info %set-breakpoint-info)
  3209.  
  3210. ;;;
  3211. ;;; BREAKPOINT-ACTIVE-P and DELETE-BREAKPOINT.
  3212. ;;;
  3213.  
  3214. ;;; BREAKPOINT-ACTIVE-P -- Public.
  3215. ;;;
  3216. (defun breakpoint-active-p (breakpoint)
  3217.   "This returns whether breakpoint is currently active."
  3218.   (ecase (breakpoint-status breakpoint)
  3219.     (:active t)
  3220.     ((:inactive :deleted) nil)))
  3221.  
  3222. ;;; DELETE-BREAKPOINT -- Public.
  3223. ;;;
  3224. (defun delete-breakpoint (breakpoint)
  3225.   "This frees system storage and removes computational overhead associated with
  3226.    breakpoint.  After calling this, breakpoint is completely impotent and can
  3227.    never become active again."
  3228.   (let ((status (breakpoint-status breakpoint)))
  3229.     (unless (eq status :deleted)
  3230.       (when (eq status :active)
  3231.     (deactivate-breakpoint breakpoint))
  3232.       (setf (breakpoint-status breakpoint) :deleted)
  3233.       (let ((other (breakpoint-unknown-return-partner breakpoint)))
  3234.     (when other
  3235.       (setf (breakpoint-status other) :deleted)))
  3236.       (when (eq (breakpoint-kind breakpoint) :function-end)
  3237.     (let* ((starter (breakpoint-start-helper breakpoint))
  3238.            (breakpoints (delete breakpoint
  3239.                     (the list (breakpoint-info starter)))))
  3240.       (setf (breakpoint-info starter) breakpoints)
  3241.       (unless breakpoints
  3242.         (delete-breakpoint starter)
  3243.         (setf (compiled-debug-function-end-starter
  3244.            (breakpoint-what breakpoint))
  3245.           nil))))))
  3246.   breakpoint)
  3247.  
  3248. ;;;
  3249. ;;; C call out stubs.
  3250. ;;;
  3251.  
  3252. ;;; BREAKPOINT-INSTALL -- Internal.
  3253. ;;;
  3254. ;;; This actually installs the break instruction in the component.  It returns
  3255. ;;; the overwritten bits.  You must call this in a context in which GC is
  3256. ;;; disabled, so Lisp doesn't move objects around that C is pointing to.
  3257. ;;;
  3258. (alien:def-alien-routine "breakpoint_install" c-call:unsigned-long
  3259.   (code-obj c-call:unsigned-long)
  3260.   (pc-offset c-call:int))
  3261.  
  3262. ;;; BREAKPOINT-REMOVE -- Internal.
  3263. ;;;
  3264. ;;; This removes the break instruction and replaces the original instruction.
  3265. ;;; You must call this in a context in which GC is disabled, so Lisp doesn't
  3266. ;;; move objects around that C is pointing to.
  3267. ;;;
  3268. (alien:def-alien-routine "breakpoint_remove" c-call:void
  3269.   (code-obj c-call:unsigned-long)
  3270.   (pc-offset c-call:int)
  3271.   (old-inst c-call:unsigned-long))
  3272.  
  3273. ;;; BREAKPOINT-AFTER-OFFSET -- Internal.
  3274. ;;;
  3275. ;;; This returns the offset of the next instruction following the break that
  3276. ;;; generated the signal context we supply as an argument to this routine.
  3277. ;;;
  3278. (alien:def-alien-routine "breakpoint_after_offset" c-call:int
  3279.   (scp system:system-area-pointer))
  3280.  
  3281. ;;;
  3282. ;;; Breakpoint handlers (layer between C and exported interface).
  3283. ;;;
  3284.  
  3285. ;;; This maps components to a mapping of offsets to breakpoint-datas.
  3286. ;;;
  3287. (defvar *component-breakpoint-offsets* (make-hash-table :test #'eq))
  3288.  
  3289. ;;; BREAKPOINT-DATA -- Internal.
  3290. ;;;
  3291. ;;; This returns the breakpoint-data associated with component cross offset.
  3292. ;;; If none exists, this makes one, installs it, and returns it.
  3293. ;;;
  3294. (defun breakpoint-data (component offset &optional (create t))
  3295.   (flet ((install-breakpoint-data ()
  3296.        (when create
  3297.          (let ((data (make-breakpoint-data component offset)))
  3298.            (push (cons offset data)
  3299.              (gethash component *component-breakpoint-offsets*))
  3300.            data))))
  3301.     (let ((offsets (gethash component *component-breakpoint-offsets*)))
  3302.       (if offsets
  3303.       (let ((data (assoc offset offsets)))
  3304.         (if data
  3305.         (cdr data)
  3306.         (install-breakpoint-data)))
  3307.       (install-breakpoint-data)))))
  3308.  
  3309. ;;; DELETE-BREAKPOINT-DATA -- Internal.
  3310. ;;;
  3311. ;;; We use this when there are no longer any active breakpoints corresponding
  3312. ;;; to data.
  3313. ;;;
  3314. (defun delete-breakpoint-data (data)
  3315.   (let* ((component (breakpoint-data-component data))
  3316.      (offsets (delete (breakpoint-data-offset data)
  3317.               (gethash component *component-breakpoint-offsets*)
  3318.               :key #'car)))
  3319.     (if offsets
  3320.     (setf (gethash component *component-breakpoint-offsets*) offsets)
  3321.     (remhash component *component-breakpoint-offsets*)))
  3322.   (ext:undefined-value))
  3323.  
  3324. ;;; HANDLE-BREAKPOINT -- Internal Interface.
  3325. ;;;
  3326. ;;; The C handler for interrupts calls this when it has a debugging-tool break
  3327. ;;; instruction.  This does NOT handle all breaks; for example, it does not
  3328. ;;; handle breaks for internal errors.
  3329. ;;;
  3330. (defun handle-breakpoint (offset component signal-context)
  3331.   (let ((data (breakpoint-data component offset)))
  3332.     (unless data
  3333.       (error "Unknown breakpoint in ~S at offset ~S."
  3334.           (debug-function-name (debug-function-from-pc component offset))
  3335.           offset))
  3336.     (let ((breakpoints (breakpoint-data-breakpoints data)))
  3337.       (if (and breakpoints
  3338.            (eq (breakpoint-kind (car breakpoints)) :function-end))
  3339.       (handle-function-end-breakpoint breakpoints data signal-context)
  3340.       (handle-breakpoint-aux breakpoints data
  3341.                  offset component signal-context)))))
  3342.  
  3343. ;;; This holds breakpoint-datas while invoking the breakpoint hooks associated
  3344. ;;; with that particular component and location.  While they are executing, if
  3345. ;;; we hit the location again, we ignore the breakpoint to avoid infinite
  3346. ;;; recursion.  Function-end breakpoints must work differently since the
  3347. ;;; breakpoint-data is unique for each invocation.
  3348. ;;;
  3349. (defvar *executing-breakpoint-hooks* nil)
  3350.  
  3351. ;;; HANDLE-BREAKPOINT-AUX -- Internal.
  3352. ;;;
  3353. ;;; This handles code-location and debug-function :function-start breakpoints.
  3354. ;;;
  3355. (defun handle-breakpoint-aux (breakpoints data offset component signal-context)
  3356.   (let ((after (breakpoint-data-after-breakpoint data)))
  3357.     (when after
  3358.       (handle-after-breakpoint after breakpoints data offset component)))
  3359.   
  3360.   (when breakpoints
  3361.     (unless (member data *executing-breakpoint-hooks*)
  3362.       (let ((*executing-breakpoint-hooks* (cons data
  3363.                         *executing-breakpoint-hooks*)))
  3364.     (invoke-breakpoint-hooks breakpoints component offset)))
  3365.     ;; At this point breakpoints may not hold the same list as
  3366.     ;; BREAKPOINT-DATA-BREAKPOINTS since invoking hooks may have allowed a
  3367.     ;; breakpoint deactivation.  In fact, if all breakpoints were deactivated
  3368.     ;; then data is invalid since it was deleted and so the correct one must be
  3369.     ;; looked up if it is to be used.  If there are no more breakpoints active
  3370.     ;; at this location, then the normal instruction has been put back, and we
  3371.     ;; do not need an after breakpoint to re-install a break instruction.
  3372.     (setf data (breakpoint-data component offset nil))
  3373.     (when (and data
  3374.            (breakpoint-data-breakpoints data))
  3375.       ;; Restore instruction.  Do this before SET-AFTER-BREAKPOINTS which
  3376.       ;; uses CALL-BREAKPOINT-AFTER-OFFSET.
  3377.       (system:without-gcing
  3378.        (breakpoint-remove (kernel:get-lisp-obj-address component) offset
  3379.               (breakpoint-data-instruction data)))
  3380.       (set-after-breakpoints component data signal-context)
  3381.       ;; Set the sigmask, to keep the system running until we can
  3382.       ;; remove the after breakpoints and re-install the user breakpoints.
  3383.       (setf (breakpoint-data-sigmask data)
  3384.         (unix:unix-sigblock (unix:sigmask :sigint :sigquit :sigtstp))))))
  3385.  
  3386. (defun handle-after-breakpoint (after breakpoints data offset component)
  3387.   (let ((previous-data (after-breakpoint-previous-data after)))
  3388.     (unless (breakpoint-data-breakpoints previous-data)
  3389.       (error "After-breakpoint found with no user breakpoints. -- ~s" after))
  3390.     ;; If there are no user breakpoints at this after location, remove the
  3391.     ;; break instruction.
  3392.     (unless breakpoints
  3393.       (system:without-gcing
  3394.        (breakpoint-remove (kernel:get-lisp-obj-address component) offset
  3395.               (breakpoint-data-instruction data)))
  3396.       (setf (breakpoint-data-after-breakpoint data) nil)
  3397.       (delete-breakpoint-data data))
  3398.     ;; Ditto for the partner of the after-breakpoint (if there were two).
  3399.     (let ((partner (after-breakpoint-partner after)))
  3400.       (when partner
  3401.     (let ((partner-data (after-breakpoint-internal-data partner)))
  3402.       (unless (breakpoint-data-breakpoints partner-data)
  3403.         (system:without-gcing
  3404.          (breakpoint-remove (kernel:get-lisp-obj-address
  3405.                  (breakpoint-data-component partner-data))
  3406.                 (breakpoint-data-offset partner-data)
  3407.                 (breakpoint-data-instruction partner-data)))
  3408.         (setf (breakpoint-data-after-breakpoint partner-data) nil)
  3409.         (delete-breakpoint-data partner-data)))))
  3410.     ;; Put back previous's break instruction.
  3411.     ;; We don't need to store the replaced instruction in the data since we
  3412.     ;; have it from installing the break instruction before.
  3413.     (system:without-gcing
  3414.      (breakpoint-install (kernel:get-lisp-obj-address
  3415.               (breakpoint-data-component previous-data))
  3416.              (breakpoint-data-offset previous-data)))
  3417.     ;; Restore sigmask that we saved before executing previous's inst.
  3418.     (unix:unix-sigsetmask (breakpoint-data-sigmask previous-data))))
  3419.  
  3420. (defun invoke-breakpoint-hooks (breakpoints component offset)
  3421.   (let* ((debug-fun (debug-function-from-pc component offset))
  3422.      (frame (do ((f (top-frame) (frame-down f)))
  3423.             ((eq debug-fun (frame-debug-function f)) f)))) 
  3424.     (dolist (bpt breakpoints)
  3425.       (funcall (breakpoint-hook-function bpt)
  3426.            frame
  3427.            ;; If this is an :unknown-return-partner, then pass the
  3428.            ;; hook function the original breakpoint, so that users
  3429.            ;; arn't forced to confront the fact that some breakpoints
  3430.            ;; really are two.
  3431.            (if (eq (breakpoint-kind bpt) :unknown-return-partner)
  3432.            (breakpoint-unknown-return-partner bpt)
  3433.            bpt)))))
  3434.  
  3435. (defun set-after-breakpoints (component data signal-context)
  3436.   (multiple-value-bind (after-1 after-2)
  3437.                (call-breakpoint-after-offset signal-context)
  3438.     (let* ((after-data-1 (breakpoint-data component after-1))
  3439.        (after-data-2 (if (not (zerop after-2))
  3440.                  (breakpoint-data component after-2)))
  3441.        (after-bpt-1 (make-after-breakpoint data after-data-1)))
  3442.       (setf (breakpoint-data-after-breakpoint after-data-1) after-bpt-1)
  3443.       (when after-data-2
  3444.     ;; Set after-bpt-1's partner to an after-breakpoint.
  3445.     (setf (after-breakpoint-partner after-bpt-1)
  3446.           ;; While making it, store it in the appropriate data obj.
  3447.           (setf (breakpoint-data-after-breakpoint after-data-2)
  3448.             (make-after-breakpoint data after-data-2 after-bpt-1))))
  3449.       (system:without-gcing
  3450.        (unless (breakpoint-data-breakpoints after-data-1)
  3451.      (setf (breakpoint-data-instruction after-data-1)
  3452.            (breakpoint-install (kernel:get-lisp-obj-address component)
  3453.                    after-1)))
  3454.        (when (and after-data-2 (not (breakpoint-data-breakpoints after-data-2)))
  3455.      (setf (breakpoint-data-instruction after-data-2)
  3456.            (breakpoint-install (kernel:get-lisp-obj-address component)
  3457.                    after-2)))))))
  3458.  
  3459. ;;; HANDLE-FUNCTION-END-BREAKPOINT -- Internal.
  3460. ;;;
  3461. ;;; HANDLE-BREAKPOINT calls this for :function-end breakpoints.
  3462. ;;;
  3463. (defun handle-function-end-breakpoint (breakpoints data signal-context)
  3464.   (delete-breakpoint-data data)
  3465.   (let* ((scp (alien:sap-alien signal-context (* unix:sigcontext)))
  3466.      (frame (do ((cfp (vm:sigcontext-register scp vm::cfp-offset))
  3467.              (f (top-frame) (frame-down f)))
  3468.             ((= cfp (system:sap-int (frame-pointer f))) f)
  3469.           (declare (type (unsigned-byte #.vm:word-bits) cfp))))
  3470.      (component (breakpoint-data-component data))
  3471.      (cookie (gethash component *function-end-cookies*)))
  3472.     (remhash component *function-end-cookies*)
  3473.     (dolist (bpt breakpoints)
  3474.       (funcall (breakpoint-hook-function bpt)
  3475.            frame bpt
  3476.            (get-function-end-breakpoint-values scp)
  3477.            cookie))))
  3478.  
  3479. (defun get-function-end-breakpoint-values (scp)
  3480.   (let ((ocfp (system:int-sap (vm:sigcontext-register scp vm::ocfp-offset)))
  3481.     (nargs (kernel:make-lisp-obj
  3482.         (vm:sigcontext-register scp vm::nargs-offset)))
  3483.     (reg-arg-offsets vm::register-arg-offsets)
  3484.     (results nil))
  3485.     (system:without-gcing
  3486.      (dotimes (arg-num nargs)
  3487.        (push (if reg-arg-offsets
  3488.          (kernel:make-lisp-obj
  3489.           (vm:sigcontext-register scp (pop reg-arg-offsets)))
  3490.          (kernel:stack-ref ocfp arg-num))
  3491.          results)))
  3492.     (nreverse results)))
  3493.  
  3494.  
  3495. ;;; CALL-BREAKPOINT-AFTER-OFFSET -- Internal.
  3496. ;;;
  3497. ;;; This calls the C routine and massages its return values.  Originally the
  3498. ;;; breakpoint code was designed for the C code to return multiple offsets when
  3499. ;;; the break generating the argument signal-context had replaced a branch
  3500. ;;; instruction.  The Lisp code would then set after-breakpoints at each
  3501. ;;; location, cleaning up both when one was hit after continuing execution.
  3502. ;;; Later the C code decided it could determine which way the branch would go,
  3503. ;;; so BREAKPOINT-AFTER-OFFSET could just return one offset.  In case this
  3504. ;;; won't be possible on all platforms, the Lisp code will stay with its
  3505. ;;; support for multiple after-breakpoints.  Then we only need to change this
  3506. ;;; routine to return all values of BREAKPOINT-AFTER-OFFSET.
  3507. ;;;
  3508. (defun call-breakpoint-after-offset (signal-context)
  3509.   (values (breakpoint-after-offset signal-context) 0))
  3510.  
  3511. ;;;
  3512. ;;; MAKE-BOGUS-LRA (used for :function-end breakpoints)
  3513. ;;;
  3514.  
  3515. (defconstant bogus-lra-constants 2)
  3516. (defconstant known-return-p-slot (+ vm:code-constants-offset 1))
  3517.  
  3518. ;;; MAKE-BOGUS-LRA -- Interface.
  3519. ;;;
  3520. (defun make-bogus-lra (real-lra &optional known-return-p)
  3521.   "Make a bogus LRA object that signals a breakpoint trap when returned to.  If
  3522.    the breakpoint trap handler returns, REAL-LRA is returned to.  Three values
  3523.    are returned: the bogus LRA object, the code component it is part of, and
  3524.    the PC offset for the trap instruction."
  3525.   (system:without-gcing
  3526.    (let* ((src-start (system:foreign-symbol-address
  3527.               "function_end_breakpoint_guts"))
  3528.       (src-end (system:foreign-symbol-address
  3529.             "function_end_breakpoint_end"))
  3530.       (trap-loc (system:foreign-symbol-address
  3531.              "function_end_breakpoint_trap"))
  3532.       (length (system:sap- src-end src-start))
  3533.       (code-object (system:%primitive c:allocate-code-object
  3534.                       (1+ bogus-lra-constants)
  3535.                       length))
  3536.       (dst-start (kernel:code-instructions code-object)))
  3537.      (declare (type system:system-area-pointer
  3538.             src-start src-end dst-start trap-loc)
  3539.           (type kernel:index length))
  3540.      (setf (kernel:code-header-ref code-object vm:code-debug-info-slot)
  3541.        :bogus-lra)
  3542.      (setf (kernel:code-header-ref code-object vm:code-trace-table-offset-slot)
  3543.        length)
  3544.      (setf (kernel:code-header-ref code-object real-lra-slot) real-lra)
  3545.      (setf (kernel:code-header-ref code-object known-return-p-slot)
  3546.        known-return-p)
  3547.      (kernel:system-area-copy src-start 0 dst-start 0 (* length vm:byte-bits))
  3548.      (let ((new-lra (kernel:make-lisp-obj (+ (system:sap-int dst-start)
  3549.                          vm:other-pointer-type))))
  3550.        (kernel:set-header-data
  3551.     new-lra
  3552.     (logandc2 (+ vm:code-constants-offset bogus-lra-constants 1)
  3553.           1))
  3554.        (values new-lra code-object (system:sap- trap-loc src-start))))))
  3555.  
  3556.  
  3557.  
  3558. ;;;; Editor support.
  3559.  
  3560. ;;; This holds breakpoints in the slave set on behalf of the editor.
  3561. ;;;
  3562. ;(defvar *editor-breakpoints* (make-hash-table :test #'equal))
  3563.  
  3564. ;;;
  3565. ;;; Setting breakpoints.
  3566. ;;;
  3567.  
  3568. ;;; SET-BREAKPOINT-FOR-EDITOR -- Internal Interface.
  3569. ;;;
  3570. (defun set-breakpoint-for-editor (package name-str path)
  3571.   "The editor calls this remotely in the slave to set breakpoints.  Package is
  3572.    the string name of a package or nil, and name-str is a string representing a
  3573.    function name (for example, \"foo\" or \"(setf foo)\").  After finding
  3574.    package, this READs name-str with *package* bound appropriately.  Path is
  3575.    either a modified source-path or a symbol (:function-start or
  3576.    :function-end).  If it is a modified source-path, it has no top-level-form
  3577.    offset or form-number component, and it is in descent order from the root of
  3578.    the top-level form."
  3579.   (let* ((name (let ((*package* (if package
  3580.                     (lisp::package-or-lose package)
  3581.                     *package*)))
  3582.          (read-from-string name-str)))
  3583.      (debug-fun (function-debug-function (fdefinition name))))
  3584.     (etypecase path
  3585.       (symbol
  3586.        (let* ((bpt (di:make-breakpoint
  3587.             #'(lambda (frame bpt)
  3588.             (declare (ignore frame bpt))
  3589.             (break "Editor installed breakpoint."))
  3590.             debug-fun :kind path))
  3591.           (remote-bpt (wire:make-remote-object bpt)))
  3592.      (activate-breakpoint bpt)
  3593.      ;;(push remote-bpt (gethash name *editor-breakpoints*))
  3594.      remote-bpt))
  3595.       (cons
  3596.        (etypecase debug-fun
  3597.      (compiled-debug-function
  3598.       (compiled-debug-function-set-breakpoint-for-editor
  3599.        debug-fun #|name|# path))
  3600.      (interpreted-debug-function
  3601.       (error
  3602.        "We don't currently support breakpoints in interpreted code.")))))))
  3603.  
  3604. (defun compiled-debug-function-set-breakpoint-for-editor (debug-fun #|name|# path)
  3605.   (let* ((source-paths (generate-component-source-paths
  3606.             (compiled-debug-function-component debug-fun)))
  3607.      (matches nil)
  3608.      (matching-length 0))
  3609.     (declare (simple-vector source-paths)
  3610.          (list matches)
  3611.          (fixnum matching-length))
  3612.     ;; Build a list of paths that match path up to matching-length
  3613.     ;; elements.
  3614.     (macrolet ((maybe-store-match (path matched-len)
  3615.          `(cond ((> ,matched-len matching-length)
  3616.              (setf matches (list ,path))
  3617.              (setf matching-length ,matched-len))
  3618.             ((= ,matched-len matching-length)
  3619.              (cons ,path matches)))))
  3620.       (dotimes (i (length source-paths))
  3621.     (declare (fixnum i))
  3622.     (let ((sp (svref source-paths i)))
  3623.       ;; Remember, first element of sp is a code-location.
  3624.       (do ((path-ptr path (cdr path-ptr))
  3625.            (sp-ptr (cdr sp) (cdr sp-ptr))
  3626.            (count 0 (1+ count)))
  3627.           ((or (null path-ptr) (null sp-ptr))
  3628.            (when (null sp-ptr)
  3629.          (maybe-store-match sp count)))
  3630.         (declare (list sp-ptr path-ptr)
  3631.              (fixnum count))
  3632.         (unless (= (the fixnum (car path-ptr)) (the fixnum (car sp-ptr)))
  3633.           (maybe-store-match sp count))))))
  3634.     ;; If there's just one, set it; otherwise, return the conflict set.
  3635.     (cond ((and (= (length matches) 1) (equal path (cdar matches)))
  3636.        (let* ((bpt (make-breakpoint
  3637.             #'(lambda (frame bpt)
  3638.                 (declare (ignore frame bpt))
  3639.                 (break "Editor installed breakpoint."))
  3640.             (wire:remote-object-value (caar matches))))
  3641.           (remote-bpt (wire:make-remote-object bpt)))
  3642.          (activate-breakpoint bpt)
  3643.          ;;(push remote-bpt (gethash name *editor-breakpoints*))
  3644.          remote-bpt))
  3645.       (t matches))))
  3646.  
  3647. ;;; This maps components to vectors of modified source-paths.  We assume users
  3648. ;;; will set multiple breakpoints in a given function which entails computing
  3649. ;;; this data repeatedly.  Possibly the GC hook should free this cache.  The
  3650. ;;; source-paths are modified in the following ways:
  3651. ;;;    1] The form number element (first) is clobbered with the code-location
  3652. ;;;       corresponding to the source-path.
  3653. ;;;    2] The top-level-form offset element (last) is thrown away.
  3654. ;;;    3] Everything after the first element is reversed, so the modified
  3655. ;;;       source-path actually portrays a descent into the form.
  3656. ;;;
  3657. (defvar *component-source-locations* (make-hash-table :test #'eq))
  3658.  
  3659. ;;; GENERATE-COMPONENT-SOURCE-PATHS -- Internal.
  3660. ;;;
  3661. ;;; This returns a vector of modified source-paths, one for every code-location
  3662. ;;; in component.  The source-paths are modified as described for
  3663. ;;; *component-source-locations*.
  3664. ;;;
  3665. (defun generate-component-source-paths (component)
  3666.   (or (gethash component *component-source-locations*)
  3667.       (setf (gethash component *component-source-locations*)
  3668.         (sub-generate-component-source-paths component))))
  3669.  
  3670. ;;; This maps source-infos to hashtables that map top-level-form offsets to
  3671. ;;; modified form-number translations (as returned by
  3672. ;;; FORM-NUMBER-TRANSLATIONS).  These are modified as described for
  3673. ;;; *component-source-locations*.
  3674. ;;;
  3675. (defvar *source-info-offset-translations* (make-hash-table :test #'eq))
  3676.  
  3677. ;;; This is a hacking space for SUB-GENERATE-COMPONENT-SOURCE-PATHS.  We use
  3678. ;;; this because we throw away many source-paths we accumulate in this buffer
  3679. ;;; since they are not associated with code-locations.
  3680. ;;;
  3681. (defvar *source-paths-buffer* (make-array 50 :fill-pointer t :adjustable t))
  3682.  
  3683. ;;; SUB-GENERATE-COMPONENT-SOURCE-PATHS -- Internal.
  3684. ;;;
  3685. ;;; We iterate over the code-locations in component, fetching their
  3686. ;;; source-infos and using the *source-info-offset-translations* cache.  This
  3687. ;;; computation often repeatedly sees the same source-info/tlf-offset pair, so
  3688. ;;; we see many source-paths from one form-number-translation table.  Because
  3689. ;;; of this, when we add a form-number-translations table to this cache, we add
  3690. ;;; all the source-paths in it to the result immediately.  Then later if we see
  3691. ;;; the same (not EQ though) source-info/tlf-offset form-number-translations,
  3692. ;;; we can simply check if one of the source-paths is already in the result,
  3693. ;;; and if it is, then all of them already are.  We keep the cache around
  3694. ;;; between invocations since we expect multiple breakpoints to be set in the
  3695. ;;; same function, and this is why we must check if a form-number-translations
  3696. ;;; has been added to the result; just its presence in the cache does not mean
  3697. ;;; it is in the result vector.
  3698. ;;;
  3699. (defun sub-generate-component-source-paths (component)
  3700.   (let ((info (kernel:code-debug-info component)))
  3701.     (unless info (debug-signal 'no-debug-info))
  3702.     (let* ((function-map (get-debug-info-function-map info))
  3703.        (result *source-paths-buffer*))
  3704.       (declare (simple-vector function-map)
  3705.            (vector result))
  3706.       (setf (fill-pointer result) 0)
  3707.       (flet ((copy-stuff (form-num-trans result)
  3708.            (declare (simple-vector form-num-trans)
  3709.             (vector result))
  3710.            (dotimes (i (length form-num-trans))
  3711.          (declare (fixnum i))
  3712.          (vector-push-extend (svref form-num-trans i) result)))
  3713.          (convert-paths (form-num-trans)
  3714.            (declare (simple-vector form-num-trans))
  3715.            (dotimes (i (length form-num-trans) form-num-trans)
  3716.          (declare (fixnum i))
  3717.          (let* ((source-path (svref form-num-trans i)))
  3718.            (declare (list source-path))
  3719.            ;; Make the first cons point to the reversal of everything
  3720.            ;; else, but throw away what was the last element before the
  3721.            ;; reversal.
  3722.            (setf (cdr source-path)
  3723.              ;; Must copy the rest of the list, so REVERSE, but
  3724.              ;; the first cons cell of each list is unique.
  3725.              (cdr (reverse (cdr source-path))))))))
  3726.     ;; Get all possible source-paths, modifying any new additions to the
  3727.     ;; cache.
  3728.     (do ((i 0 (+ i 2))
  3729.          (len (length function-map)))
  3730.         ((>= i len))
  3731.       (declare (type c::index i))
  3732.       (let ((d-fun (make-compiled-debug-function (svref function-map i)
  3733.                              component)))
  3734.         (do-debug-function-blocks (d-block d-fun)
  3735.           (do-debug-block-locations (loc d-block)
  3736.         (let* ((d-source (code-location-debug-source loc))
  3737.                (translations (gethash d-source
  3738.                           *source-info-offset-translations*))
  3739.                (tlf-offset (code-location-top-level-form-offset loc))
  3740.                (loc-num (code-location-form-number loc)))
  3741.           (cond
  3742.            (translations
  3743.             (let ((form-num-trans (gethash tlf-offset translations)))
  3744.               (declare (type (or simple-vector null) form-num-trans))
  3745.               (cond
  3746.                ((not form-num-trans)
  3747.             (let ((form-num-trans (get-form-number-translations
  3748.                            d-source tlf-offset)))
  3749.               (declare (simple-vector form-num-trans))
  3750.               (setf (gethash tlf-offset translations) form-num-trans)
  3751.               (copy-stuff (convert-paths form-num-trans) result)
  3752.               (setf (car (svref form-num-trans loc-num))
  3753.                 (wire:make-remote-object loc))))
  3754.                ;; If one of these source-paths is in our result, then
  3755.                ;; they all are.
  3756.                ((find (svref form-num-trans 0) result :test #'eq)
  3757.             (setf (car (svref form-num-trans loc-num))
  3758.                   (wire:make-remote-object loc)))
  3759.                ;; Otherwise, store these source-paths in the result.
  3760.                (t
  3761.             (copy-stuff form-num-trans result)
  3762.             (setf (car (svref form-num-trans loc-num))
  3763.                   (wire:make-remote-object loc))))))
  3764.            (t
  3765.             (let ((translations (make-hash-table :test #'eq))
  3766.               (form-num-trans (get-form-number-translations
  3767.                        d-source tlf-offset)))
  3768.               (declare (simple-vector form-num-trans))
  3769.               (setf (gethash d-source *source-info-offset-translations*)
  3770.                 translations)
  3771.               (setf (gethash tlf-offset translations) form-num-trans)
  3772.               (copy-stuff (convert-paths form-num-trans) result)
  3773.               (setf (car (svref form-num-trans loc-num))
  3774.                 (wire:make-remote-object loc)))))))))))
  3775.       ;; Copy source-paths with code-locations from the result buffer to a
  3776.       ;; real result vector.
  3777.       (let* ((count (count-if #'(lambda (x) (wire:remote-object-p (car x)))
  3778.                   result))
  3779.          (the-real-thing (make-array count))
  3780.          (i -1))
  3781.     (declare (simple-vector the-real-thing)
  3782.          (fixnum i count))
  3783.     (dotimes (j count)
  3784.       (loop (when (wire:remote-object-p (car (aref result (incf i))))
  3785.           (return)))
  3786.       (setf (svref the-real-thing j) (aref result i)))
  3787.     the-real-thing))))
  3788.  
  3789. ;;; GET-FORM-NUMBER-TRANSLATIONS -- Internal.
  3790. ;;;
  3791. ;;; This returns a vector of form-number translations to source-paths for
  3792. ;;; d-source and the top-level-form indicated by the top-level-form offset.
  3793. ;;;
  3794. (defun get-form-number-translations (d-source tlf-offset)
  3795.   (let ((name (debug-source-name d-source)))
  3796.     (ecase (debug-source-from d-source)
  3797.       (:file
  3798.        (cond
  3799.     ((not (probe-file name))
  3800.      (format t "~%Cannot set breakpoints for editor when source file no ~
  3801.             longer exists:~%  ~A."
  3802.          (namestring name)))
  3803.     (t
  3804.      (let* ((local-tlf-offset (- tlf-offset
  3805.                      (debug-source-root-number d-source)))
  3806.         (char-offset
  3807.          (aref (or (debug-source-start-positions d-source)
  3808.                (error "Cannot set breakpoints for editor when ~
  3809.                    there is no start positions map."))
  3810.                local-tlf-offset)))
  3811.        (with-open-file (f name)
  3812.          (cond
  3813.           ((= (debug-source-created d-source) (file-write-date name))
  3814.            (file-position f char-offset))
  3815.           (t
  3816.            (format t
  3817.                "~%While setting a breakpoint for the editor, noticed ~
  3818.             source file has been modified since compilation:~%  ~A~@
  3819.             Using form offset instead of character position.~%"
  3820.                (namestring name))
  3821.            (dotimes (i local-tlf-offset) (read f))))
  3822.          (form-number-translations (read f) tlf-offset))))))
  3823.       ((:lisp :stream)
  3824.        (form-number-translations (svref name tlf-offset) tlf-offset)))))
  3825.  
  3826. ;;; SET-LOCATION-BREAKPOINT-FOR-EDITOR -- Internal Interface.
  3827. ;;;
  3828. (defun set-location-breakpoint-for-editor (remote-obj-loc)
  3829.   "The editor calls this in the slave with a remote-object representing a
  3830.    code-location to set a breakpoint."
  3831.   (let ((loc (wire:remote-object-value remote-obj-loc)))
  3832.     (etypecase loc
  3833.       (interpreted-code-location
  3834.        (error "Breakpoints in interpreted code are currently unsupported."))
  3835.       (compiled-code-location
  3836.        (let* ((bpt (make-breakpoint #'(lambda (frame bpt)
  3837.                     (declare (ignore frame bpt))
  3838.                     (break "Editor installed breakpoint."))
  3839.                     loc))
  3840.           (remote-bpt (wire:make-remote-object bpt)))
  3841.      (activate-breakpoint bpt)
  3842.      ;;(push remote-bpt (gethash name *editor-breakpoints*))
  3843.      remote-bpt)))))
  3844.  
  3845. ;;;
  3846. ;;; Deleting breakpoints.
  3847. ;;;
  3848.  
  3849. ;;; DELETE-BREAKPOINT-FOR-EDITOR -- Internal Interface.
  3850. ;;;
  3851. (defun delete-breakpoint-for-editor (remote-obj-bpt)
  3852.   "The editor calls this remotely in the slave to delete a breakpoint."
  3853.   (delete-breakpoint (wire:remote-object-value remote-obj-bpt))
  3854.   (wire:forget-remote-translation remote-obj-bpt))
  3855.  
  3856.  
  3857.  
  3858. ;;;; Miscellaneous
  3859.  
  3860. ;;; This appears here because it cannot go with the debug-function interface
  3861. ;;; since DO-DEBUG-BLOCK-LOCATIONS isn't defined until after the debug-function
  3862. ;;; routines.
  3863. ;;;
  3864.  
  3865. ;;; DEBUG-FUNCTION-START-LOCATION -- Public.
  3866. ;;;
  3867. (defun debug-function-start-location (debug-fun)
  3868.   "This returns a code-location before the body of a function and after all
  3869.    the arguments are in place.  If this cannot determine that location due to
  3870.    a lack of debug information, it returns nil."
  3871.   (etypecase debug-fun
  3872.     (compiled-debug-function
  3873.      (code-location-from-pc debug-fun
  3874.                 (c::compiled-debug-function-start-pc
  3875.                  (compiled-debug-function-compiler-debug-fun
  3876.                   debug-fun))
  3877.                 nil))
  3878.     (interpreted-debug-function
  3879.      ;; Return the first location if there are any, otherwise nil.
  3880.      (handler-case (do-debug-function-blocks (block debug-fun nil)
  3881.              (do-debug-block-locations (loc block nil)
  3882.                (return-from debug-function-start-location loc)))
  3883.        (no-debug-blocks (condx)
  3884.      (declare (ignore condx))
  3885.      nil)))))
  3886.  
  3887.  
  3888.  
  3889. (defun print-code-locations (function)
  3890.   (let ((debug-fun (function-debug-function function)))
  3891.     (do-debug-function-blocks (block debug-fun)
  3892.       (do-debug-block-locations (loc block)
  3893.     (fill-in-code-location loc)
  3894.     (format t "~S code location at ~D"
  3895.         (compiled-code-location-kind loc)
  3896.         (compiled-code-location-pc loc))
  3897.     (debug::print-code-location-source-form loc 0)
  3898.     (terpri)))))
  3899.